mysql_fetch_array 错误 - 不是有效的 MySQL 结果资源
我对 PHP 很陌生,试图编写一个连接到 MySQL 数据库的脚本,并简单地在每个标题下以列表格式显示内容;
我的表包含 ID(自动增量)、FName、SName 和E 地址字段。
数据库名为 iphonehe_MGFSales,用户名是 iphonehe_MGFSale - 我已将用户添加到具有完全权限的数据库中。
我正在尝试使用 mysql 函数和此代码建立与数据库的连接;
mysql_connect ("localhost", "iphonehe_MGFSale", "xxxxxxx") or die ('I cannot connect to the database because: ' . mysql_error());
mysql_select_db ("iphonehe_MGFSales");
我创建的表称为 MGFSales DB。我正在使用此代码来尝试构建查询;
$query = mysql_query("SELECT * FROM MGFSales_DB");
最后我尝试使用以下代码显示结果;
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 />";
}
我已将文件命名为index.php并上传到我的服务器,运行时出现以下错误“警告:mysql_fetch_array():提供的参数不是/home/iphonehe/public_html/pauldmorris.co.uk中的有效MySQL结果资源” /mgf/index.php on line 16'
有人指出我正确的方向吗?从我所看到的来看,我的代码的第 16 行似乎非常紧凑,我是否忽略了某些东西? 谢谢
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;
My table contains an ID (AutoIncrement), FName, SName & EAddress fields.
The database is called iphonehe_MGFSales and the username is iphonehe_MGFSale - I have added the user to the DB with full privileges.
I am trying to establish my connection to the DB using the mysql function with this code;
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;
$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'
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
该错误仅出现在
mysql_fetch_array
行中,纠正此问题应该是故障排除的第一步。更改 mysql_fetch_array ,因为
mysql 或 php 中的最新更新不接受 mysql 而只接受 mysqli。还要将代码中的所有 mysql 更改为 mysqli 。
The error is in
mysql_fetch_array
line only, correcting this should be first step in troubleshooting.change
mysql_fetch_array
tobecuase the latest updates in mysql or php does not accept mysql but accepts only mysqli. Also change everywhere mysql to mysqli in your code.
这是因为在 $query 中发现了空资源。
您需要像下面的代码一样进行检查
,或者您也可以参考 此链接
尝试一下可能会对您有所帮助。
谢谢。
this is because of null resource found in $query..
you need to check this like below code
OR you can also refer this link
Try this may help you.
Thanks.
mysql_query 失败时返回 false,这将产生您在 mysql_fetch_array 期间遇到的错误。
请在您的代码中添加一些错误检查,并打印/记录错误消息 - 在不知道源错误是什么的情况下无法提供更多帮助。
mysql_query
returns false when it fails, which will produce the error you're getting duringmysql_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.
检查
mysql_select_db
上的返回代码。Check the return code on
mysql_select_db
.你不应该直接应用mysql_fetch_array..
你应该首先检查数据..
它检查是否没有数据,然后它将执行else块,否则你将顺利执行...
You should not apply mysql_fetch_array directly..
you should first check for data ..
it checks if there is no data then it will execute else block other wise you will have smooth execution ...