mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:何解?

惜夕 2012-05-17 11:51:16
mysql_fetch_array() expects parameter 1 to be resource, boolean given in D:\xampp\htdocs\4.php on line 12

请问是什么错误?如何改正?谢谢大家啊~

代码如下:

<?php
$con = mysql_connect("localhost","root ","");
//通过服务器locahost建立连接,用户名为root,无密码

if (!$con)
{
die('Could not connect: ' . mysql_error());
}//如果不成功,显示错误
mysql_select_db("crms", $con);//选择数据库
$result = mysql_query("SELECT * FROM Cno");//查找

while($row = mysql_fetch_array($result))//打印
{
echo $row['Cno'] . " " . $row['Cno'];
echo "<br />";
}

mysql_close($con);
?>
...全文
18012 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
如楼上所说,然后:

$sql="SELECT * FROM Cno";
$result = mysql_query($sql,$con);//查找
if(!$result)
die("SQL: {$sql}<br>Error:".mysql_error());
惜夕 2012-05-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
PHP code
if (!mysql_select_db("crms", $con)) die(mysql_error()); //要么是数据库不存在,要么就是下一行查询中的Cno表不存在
[/Quote]

啊,太粗心了···数据库是crms 数据表是classroom 包含Cno、size·····但是改成下面这样还不行啊
<?php
$con = mysql_connect("localhost","root ","");
//通过服务器locahost建立连接,用户名为root,无密码

if (!$con)
{
die('Could not connect: ' . mysql_error());
}//如果不成功,显示错误
mysql_select_db("crms", $con);//选择数据库
$result = mysql_query("SELECT * FROM classroom");//查找

while($row = mysql_fetch_array($result))//打印
{
echo $row['Cno'] . " " . $row['size'];
echo "<br />";
}

mysql_close($con);
?>
一起混吧 2012-05-17
  • 打赏
  • 举报
回复
$result = mysql_query("SELECT * FROM Cno") or die(mysql_error());
这样提示什么错误
乌镇程序员 2012-05-17
  • 打赏
  • 举报
回复
if (!mysql_select_db("crms", $con)) die(mysql_error()); //要么是数据库不存在,要么就是下一行查询中的Cno表不存在
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 的回复:]

Access denied for user ''@'localhost' to database 'crms'

这样的错误该怎么改呢?
[/Quote]
提示在服务器名"localhost"下,用 用户名""无法访问数据库"crms"
在mysql数据库中的user表中查看host字段值为“localhost”的数据,确认其user,password字段值均非空。然后:

$con = mysql_connect("localhost","root ","");
惜夕 2012-05-17
  • 打赏
  • 举报
回复
Access denied for user ''@'localhost' to database 'crms'

这样的错误该怎么改呢?
zalvsa 2012-05-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 的回复:]
引用 1 楼 的回复:
PHP code
if (!mysql_select_db("crms", $con)) die(mysql_error()); //要么是数据库不存在,要么就是下一行查询中的Cno表不存在


啊,太粗心了···数据库是crms 数据表是classroom 包含Cno、size·····但是改成下面这样还不行啊
<?php
$con = mysql_conn……
[/Quote]

mysql_query() or die (mysql_error());
在这里如果sql_query出错就会停下来,这样就不会等到mysql_fetch_array再报错误讯息了
qq120848369 2012-05-17
  • 打赏
  • 举报
回复
因为mysql_query失败返回false了。

21,886

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧