mysql_fetch_array 错误 - 不是有效的 MySQL 结果资源

发布于 2024-11-26 18:17:54 字数 1038 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

放肆 2024-12-03 18:17:54

该错误仅出现在 mysql_fetch_array 行中,纠正此问题应该是故障排除的第一步。

更改 mysql_fetch_array ,因为

mysqli_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 to

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.

帅气尐潴 2024-12-03 18:17:54

这是因为在 $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 />";
 }
}

,或者您也可以参考 此链接

尝试一下可能会对您有所帮助。

谢谢。

this is because of null resource found in $query..
you need to check this like below code

$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.

血之狂魔 2024-12-03 18:17:54

mysql_query 失败时返回 false,这将产生您在 mysql_fetch_array 期间遇到的错误。

请在您的代码中添加一些错误检查,并打印/记录错误消息 - 在不知道源错误是什么的情况下无法提供更多帮助。

mysql_query returns false when it fails, which will produce the error you're getting during 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.

烟燃烟灭 2024-12-03 18:17:54

检查 mysql_select_db 上的返回代码。

Check the return code on mysql_select_db.

音盲 2024-12-03 18:17:54

你不应该直接应用mysql_fetch_array..

你应该首先检查数据..

if(mysql_num_row($query)>0){
   your code   
}
else{
   echo 'it brings no data....';
}

它检查是否没有数据,然后它将执行else块,否则你将顺利执行...

You should not apply mysql_fetch_array directly..

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 ...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文