欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

php连接mysql二种方法 mysql乱码问题

程序员文章站 2024-01-19 09:03:46
...
  1. //在conf/php.ini中,在vim用"/php_mysql"搜索到extension=php_mysql.dll,去掉前面的";",同时在下面增加extension=php_mysqli.dll;
  2. $mysqli = new mysqli("127.0.0.1","用户名", 密码","库名");
  3. $query="select * from 表 order by theindex desc";
  4. $mysqli->query("set names gb2312");//注意此处不加会乱码
  5. $result = $mysqli->query($query);
  6. //printf() 函数输出格式化的字符串
  7. while(list($name, $theindex) = $result->fetch_row())
  8. echo("
    ".$name.$theindex);
  9. $con = mysql_connect("localhost", "用户名", "密码");
  10. if ($con) {
  11. mysql_query("set names 'gb2312'");
  12. mysql_select_db("库名", $con);//注意此处不加会乱码
  13. $rs = mysql_query("select * from 表 order by theindex desc;", $con);
  14. if ($rs) {
  15. echo ("");
  16. while($row = mysql_fetch_assoc($rs))
  17. {
  18. echo "
  19. " .
  20. "
  21. " .
  22. "
  23. " .
  24. "
  25. ";
  26. }
  27. mysql_free_result($rs);
  28. }
  29. echo ("
  30. $row[theindex] $row[name]
    ");
  31. mysql_close($con);
  32. }
复制代码