源代码:messtest是数据库名称,gbbook是表名,在图形化界面中可以查询成功
<?php
$conn=mysql_connect("localhost","root","1234","messtest");
if (empty($conn)) {
die("不能连接数据库服务器:".mysql_connect_errror());
}
mysql_select_db('messtest');
mysql_query($conn,"setnames utf8");
mysql_query("set names 'GBK'"); //使用GBK中文编码;
$results = $conn->query("SELECT * from gbbook");
while($rows = $results->fetch_row()){
echo($rows[0]."".$rows[1]."".$rows[2]."".$rows[3]."<BR>");
}
$results->free();
mysqli_close($conn);
?>
修改之后
<?php
$conn=mysql_connect("localhost","root","1234","messtest") or die("不能连接数据库服务器:".mysql_connect_errror());
mysql_select_db("messtest",$conn);
$results = mysql_query("select * fron gbbook");
mysql_query($results,$conn);
mysql_query("setnames utf8",$conn);//,"messtest"使用中文编码;
while($rows = mysql_fetch_array($results)){
echo($rows[0]."".$rows[1]."".$rows[2]."".$rows[3]."".$rows[4]);
echo"<br />";
}
//mysql_free_sql($results);
mysql_close($conn);
?>
但是第7行mysql_fetch_array错误,不是有效地实参
改为$conn=mysql_connect("localhost","root","1234");
mysql_query($conn,"setnames utf8");
改为mysql_query("setnames utf8",$conn);
mysqli_close($conn);改为mysql_close($conn);
$results->free();//改为mysql_free_result($result);
下个php中文手册 chm版的吧追问
改过之后:$results = $conn->query("SELECT * from gbbook");显示这句有错误?能帮忙吗
追答你原问题里写的是$results = mysql_query("select * fron gbbook"); // fron 改为 from
$results = $conn->query("SELECT * from gbbook"); //改为
$results = mysql_query("SELECT * from gbbook");
//貌似你把mysql 和 mysqli的用法搞混了。。
参考资料:php中文手册
没有联接成功,需找到原因是啥
empty没用,返回false也能通过empty本回答被网友采纳
mysql_query($conn,"setnames utf8");
mysql_query(sql语句,数据库链接);你用反了