php mysql_fetch_array 错误 - 不是有效的 MySQL 结果资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6829833/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
mysql_fetch_array error - Not a valid MySQL result resource
提问by Paul Morris
I am very new to PHP, trying to write a script which connects to a MySQL database and simply displays the contents in list format under each heading;
我对 PHP 很陌生,试图编写一个连接到 MySQL 数据库的脚本,并在每个标题下以列表格式简单地显示内容;
My table contains an ID (AutoIncrement), FName, SName & EAddress fields.
我的表包含一个 ID(自动增量)、FName、SName 和 EAddress 字段。
The database is called iphonehe_MGFSales and the username is iphonehe_MGFSale - I have added the user to the DB with full privileges.
该数据库名为 iphonehe_MGFSales,用户名是 iphonehe_MGFSale - 我已将该用户添加到具有完全权限的数据库中。
I am trying to establish my connection to the DB using the mysql function with this code;
我正在尝试使用带有此代码的 mysql 函数建立与数据库的连接;
mysql_connect ("localhost", "iphonehe_MGFSale", "xxxxxxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("iphonehe_MGFSales");
The table I have created is called MGFSales DB. I am using this code to attempt to build the query;
我创建的表称为 MGFSales DB。我正在使用此代码尝试构建查询;
$query = mysql_query("SELECT * FROM MGFSales_DB");
And finally I am trying to display the results using the following code;
最后,我尝试使用以下代码显示结果;
while ($row = mysql_fetch_array ($query)) {
echo "<br /> ID: " .$row['ID']. "<br /> First Name: ".$row['FName']. "<br /> Last Name: ".$row['LName']. "<br /> Email: ".$row['EAddress']. "<br />";
}
I have named the file index.php and uploaded to my server, when running I get the following error 'Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/iphonehe/public_html/pauldmorris.co.uk/mgf/index.php on line 16'
我已将文件 index.php 命名并上传到我的服务器,运行时出现以下错误“警告:mysql_fetch_array():提供的参数不是 /home/iphonehe/public_html/pauldmorris.co.uk 中的有效 MySQL 结果资源” /mgf/index.php 第 16 行'
Anyone point me in the right direction? Line 16 of my code seems pretty tight from what I can see, am i overlooking something? Thanks
有人指出我正确的方向吗?从我所见,我的代码的第 16 行似乎很紧凑,我是否忽略了什么?谢谢
回答by lovenish
The error is in mysql_fetch_array
line only, correcting this should be first step in troubleshooting.
该错误mysql_fetch_array
仅在行中,纠正此错误应该是故障排除的第一步。
change mysql_fetch_array
to
更改mysql_fetch_array
为
mysqli_fetch_array
becuase the latest updates in mysql or php does not accept mysql but accepts only mysqli. Also change everywhere mysql to mysqli in your code.
因为 mysql 或 php 中的最新更新不接受 mysql,而只接受 mysqli。还要将代码中的所有 mysql 更改为 mysqli。
回答by Chandresh M
this is because of null resource found in $query.. you need to check this like below code
这是因为在$query 中发现空资源..你需要像下面的代码一样检查这个
$query = mysql_query("SELECT * FROM MGFSales_DB"); or die("Error: ". mysql_error(). " with query ");
if(mysql_num_rows($query) > 0 ){
while ($row = mysql_fetch_array ($query)) {
echo "<br /> ID: " .$row['ID']. "<br /> First Name: ".$row['FName']. "<br /> Last Name: ".$row['LName']. "<br /> Email: ".$row['EAddress']. "<br />";
}
}
OR you can also refer this link
或者你也可以参考这个链接
Try this may help you.
试试这个可能对你有帮助。
Thanks.
谢谢。
回答by Mat
mysql_query
returns falsewhen it fails, which will produce the error you're getting during mysql_fetch_array
.
mysql_query
失败时返回false,这将产生您在mysql_fetch_array
.
Please add some error checking to your code, and print out/log the error messages - can't help any more than that without knowing what the source error is.
请在您的代码中添加一些错误检查,并打印/记录错误消息 - 在不知道源错误是什么的情况下无能为力。
回答by Scott C Wilson
Check the return code on mysql_select_db
.
检查 上的返回代码mysql_select_db
。
回答by Rukmi Patel
You should not apply mysql_fetch_array directly..
你不应该直接应用 mysql_fetch_array ..
you should first check for data ..
你应该首先检查数据..
if(mysql_num_row($query)>0){
your code
}
else{
echo 'it brings no data....';
}
it checks if there is no data then it will execute else block other wise you will have smooth execution ...
它检查是否没有数据,然后它将执行 else 块,否则您将顺利执行...