php 在 MySql 中获取资源 ID #3 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17417308/
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
Getting Resource id #3 Error in MySql
提问by user2533901
I ran this code and I got a Resource id #3 error where it should have showed the full movies table.
我运行了这段代码,我得到了一个 Resource id #3 错误,它应该显示完整的电影表。
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("treehouse_movie_db") or die(mysql_error());
$data = mysql_query("SELECT * FROM movies")
or die(mysql_error());
echo $data;
回答by alwaysLearn
This is not an error
Your query is getting executed and you are getting appropriate resource from mysql_query()
as it should be returned.
这不是error
您的查询正在执行并且您正在从中获取适当的资源,mysql_query()
因为它应该返回。
To get the response you have to use mysql_fetch_array()
or mysql_fetch_assoc()
要获得响应,您必须使用mysql_fetch_array()
或mysql_fetch_assoc()
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("treehouse_movie_db") or die(mysql_error());
$data = mysql_query("SELECT * FROM movies")
or die(mysql_error());
while($row = mysql_fetch_assoc($data))
{
print_r($row);
}
SUGGESTION:mysql_* are no longer maintained .Try switching to mysqli_*or PDO
回答by Niels Keurentjes
You're not getting an error, the MySQL API is just doing what you're asking it to: echoing the contents of $data
, which is a MySQL query resource at this point. Extend the code to actually retrieve the results:
您没有收到错误,MySQL API 只是按照您的要求执行操作:回显 的内容$data
,此时它是 MySQL 查询资源。扩展代码以实际检索结果:
while($row = mysql_fetch_object($data))
var_dump($row);
And you'll see the output.
你会看到输出。
Note that the mysql_* API is deprecated since PHP 5.5 by the way.
请注意,顺便说一下,自 PHP 5.5 起不推荐使用 mysql_* API。