PHP连接MySQL的2种方法小结以及防止乱码

yipeiwu_com5年前Mysql基础

PHP的MySQL配置

报错信息:Class 'mysqli' not found in

Answer:
1.在conf/php.ini中,在vim用"/php_mysql"搜索到extension=php_mysql.dll,去掉前面的";",

同时在下面增加extension=php_mysqli.dll;

注意后面那个dll多了个i
2."/extension_dir"检查路径是否正确;

3.找到ext/目录,把其中的php_mysql.dll,php_mysqli.dll两个文件Copy to %systemroot%/system32下.

4.重启服务

连接数据库

复制代码 代码如下:

 //在conf/php.ini中,在vim用"/php_mysql"搜索到extension=php_mysql.dll,去掉前面的";",同时在下面增加extension=php_mysqli.dll;
$mysqli = new mysqli("127.0.0.1","用户名", 密码","库名");
$query="select * from  表 order by theindex desc";
$mysqli->query("SET NAMES gb2312");//注意此处不加会乱码
$result = $mysqli->query($query);

 //printf() 函数输出格式化的字符串
while(list($name, $theindex) = $result->fetch_row())
        echo(" <br />".$name.$theindex);

$con = mysql_connect("localhost", "用户名", "密码");
if ($con) {
        mysql_query("set names 'gb2312'");
        mysql_select_db("库名", $con);//注意此处不加会乱码
                                $rs = mysql_query("select * from  表 order by theindex desc;", $con);
                                if ($rs) {
                                  echo ("<table border=1>");
                                        while($row = mysql_fetch_assoc($rs))
                                        {
                                                echo "<tr>" .
                                                "<td>$row[theindex]</td>" .
                                                "<td>$row[name]</td>" .
                                                "</tr>";
                                        }
                                        mysql_free_result($rs);
                                        }
                                        echo ("</table>");
        mysql_close($con);
}

相关文章

Excel数据导入Mysql数据库的实现代码

    首先做一下说明,为什么我要用Navicat,第一个原因,因为它是个不错的Mysql GUI工具,更重要的是,它可以将一些外部数据...

PHP MYSQL实现登陆和模糊查询两大功能

PHP MYSQL实现登陆和模糊查询两大功能

本文使用的软件版本如下:PHP版本 5.5.12;MYSQL版本 5.6.17;Apache 2.4.9 用的wampserver 一、PHPMYSQL实现登陆 一共含有两个文件:log...

php实现的mysqldb读写分离操作类示例

本文实例讲述了php实现的mysqldb读写分离操作类。分享给大家供大家参考,具体如下: /** * php MysqlDB 读写分离类 * --------------------...

PHP 和 MySQL 开发的 8 个技巧

1. PHP 中数组的使用   在操作数据库时,使用关联数组(associatively-indexed arrays)十分有帮助,下面...

PHP编程实现csv文件导入mysql数据库的方法

本文实例讲述了PHP编程实现csv文件导入mysql数据库的方法。分享给大家供大家参考,具体如下: config.db.php内容如下; <?php $username...