PHP 操作数据库乱码 以及调试
mysql> show create database pxscj;
+----------+---------------------------------------------------------------+
| Database | Create Database |
+----------+---------------------------------------------------------------+
| pxscj | CREATE DATABASE `pxscj` /*!40100 DEFAULT CHARACTER SET gbk */ |
+----------+---------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
####ex9_2.php
<?php
$conn=mysql_connect('localhost','root','') or die('连接失败');
mysql_select_db('PXSCJ', $conn) or die('选择数据库失败');
###mysql_query("SET NAMES gb2312");
mysql_query("SET NAMES utf8");
$sql="select * from XSB where 性别=0";
##$sql="select * from XSB";
###echo $sql;
###$result=mysql_query($sql);
$result = mysql_query($sql) or die(mysql_error());
echo "<table border=1>";
echo "<tr><td>学号</td><td>姓名</td><td>总学分</td></tr>";
while($row=mysql_fetch_row($result))
{
list($XH,$XM,$XB,$CSSJ,$ZY,$ZXF,$BZ)=$row;
echo "<tr><td>$XH</td><td>$XM</td><td>$ZXF</td></tr>";
}
echo "</table>";
?>
####
调试方法1
$result = mysql_query("SELECT * FROM `liebiao` WHERE leixing = '女连衣裙'",$con) or die(mysql_error());
加上or die(mysql_error()) 看看报错~
------------------------
mysql_query执行成功就返回资源形变量~否则返回false~所以造成了以上报错~
------------------------
另外,调试的时候~去掉‘@’。不然不会报错的。。
$con = @mysql_connect("localhost", "root", "liujun") or die("数据错误!"); // 这个or die就不会执行了~
调试方法2:
在执行mysql_query函数之前,要先执行mysql_query("set names gbk");