为什么在 PHP 中将 print_r() 应用于数组时会得到“Resource id #4”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1777801/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:48:45  来源:igfitidea点击:

Why do I get "Resource id #4" when I apply print_r() to an array in PHP?

phpmysqlarrays

提问by Steven

Possible Duplicate:
How do i “echo” a “Resource id #6” from a MySql response in PHP?

可能的重复:
我如何从 PHP 中的 MySql 响应“回显”“资源 ID #6”?

Below is the code:

下面是代码:

$result=mysql_query("select * from choices where a_id='$taskid'")or die(mysql_error());
print_r($result);

I get "Resource id #4", any idea?

我得到“资源 ID #4”,知道吗?

After I added

我添加后

while($row=mysql_fetch_assoc($result))
{ print_r($row); }

I just got []

我刚得到 []

What's wrong?

怎么了?

回答by Kenji Kina

You are trying to print a mysql resource variableinstead of the values contained within the resource it references. You must first try to extract the values you have gotten by using a function such as mysql_fetch_assoc().

您正在尝试打印 mysql资源变量,而不是它引用的资源中包含的值。您必须首先尝试使用诸如 mysql_fetch_assoc().

You might also try mysql_fetch_array()or mysql_fetch_row(), but I find associative arrays quite nice as they allow you to access their values by the field name as in Mike's example.

您也可以尝试mysql_fetch_array()mysql_fetch_row(),但我发现关联数组非常好,因为它们允许您按Mike 的示例中的字段名称访问它们的值。

回答by Mike B

mysql_query()does not return an array as explained in the manual. Use mysql_fetch_array(), mysql_fetch_assoc(), or mysql_fetch_row()with your $result. See the link above for more info on how to manipulate query results.

mysql_query()不返回手册中解释的数组。使用mysql_fetch_array(), mysql_fetch_assoc(), 或mysql_fetch_row()与您的$result. 有关如何操作查询结果的更多信息,请参阅上面的链接。

$result = mysql_query('SELECT * FROM table');
while ($row = mysql_fetch_assoc($result)) {
    echo $row["userid"];
    echo $row["fullname"];
    echo $row["userstatus"];
}

回答by mauris

$resultis a resource variable returned by mysql_query. More about resource variables: http://php.net/manual/en/language.types.resource.php

$result是由 返回的资源变量mysql_query。有关资源变量的更多信息:http: //php.net/manual/en/language.types.resource.php

You must use other functions such as mysql_fetch_array()or mysql_fetch_assoc()to get the array of the query resultset.

您必须使用其他函数,例如mysql_fetch_array()mysql_fetch_assoc()来获取查询结果集的数组。

$resultset = array();
$result=mysql_query("select * from choices where a_id='$taskid'") or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
  $resultset[] = $row; // fetch each row...
}
mysql_free_result($result); // optional though...

print_r($resultset);

See:

看:

http://php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/function.mysql-fetch-assoc.php
http://php.net/manual/en/function.mysql-query.php

http://php.net/manual/en/function.mysql-fetch-array.php
http://php.net/manual/en/function.mysql-fetch-assoc.php
http://php.net/手册/en/function.mysql-query.php

回答by leepowers

Resourcesare special variable types used by PHP to track external resources like database connections, file handles, sockets, etc.

资源是 PHP 用来跟踪外部资源(如数据库连接、文件句柄、套接字等)的特殊变量类型。