php 为什么这会返回资源 ID #2?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4794927/
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
Why does this return Resource id #2?
提问by Charkel
Possible Duplicate:
How do i “echo” a “Resource id #6” from a MySql response in PHP?
I am new at php and SQL and I'm trying to make the php page list the numbers of enries in the table. I'm using this code but it returns Resource id #2:
我是 php 和 SQL 的新手,我试图让 php 页面列出表中的条目数。我正在使用此代码,但它返回资源 ID #2:
$rt=mysql_query("SELECT COUNT(*) FROM persons");
echo mysql_error();
echo "<h1>Number:</h1>".$rt;
回答by svens
Because you get a mysql ressource when you do a mysql_query()
.
因为当你执行mysql_query()
.
Use something like mysql_fetch_assoc()
to get the next row. It returns an array with the column names as indices. In your case it's probably COUNT(*)
.
使用类似的东西mysql_fetch_assoc()
来获取下一行。它返回一个以列名作为索引的数组。在你的情况下,它可能是COUNT(*)
.
Here's a fix and some minor improvements of your snippet:
这是您的代码段的修复和一些小的改进:
$rt = mysql_query("SELECT COUNT(*) FROM persons") or die(mysql_error());
$row = mysql_fetch_row($rt);
if($row)
echo "<h1>Number:</h1>" . $row[0];
If you need to get all rows of the resultset use this snippet:
如果您需要获取结果集的所有行,请使用以下代码段:
while($row = mysql_fetch_assoc($rt)) {
var_dump($row);
}
回答by Bjoern
mysql_query()
doesn't return a value, it returns a resource (see herein the manual).
mysql_query()
不返回值,它返回一个资源(参见手册中的此处)。
The returned result resource should be passed to another function for dealing with result tables (like mysql_fetch_array()or mysql_fetch_assoc()), to access the returned data.
返回的结果资源应该传递给另一个处理结果表的函数(如mysql_fetch_array()或mysql_fetch_assoc()),以访问返回的数据。
Example based on your initial code:
基于您的初始代码的示例:
$rt=mysql_query("SELECT COUNT(*) FROM persons");
while($row = mysql_fetch_assoc($rt)) {
var_dump($row);
}
回答by Pekkasso
Try this:
尝试这个:
$rt=mysql_query("SELECT COUNT(*) FROM persons");
echo mysql_error();
$count = mysql_result($rt, 0, 0);
echo $count;
回答by Dominic Barnes
In PHP, resourcesare returned from certain functions so that they can be passed to other related functions. Examples include database connections, database query results, file-handles, etc.
在 PHP 中,资源从某些函数返回,以便它们可以传递给其他相关函数。示例包括数据库连接、数据库查询结果、文件句柄等。
According to the documentation on mysql_query()
, a SELECT query returns a resource. You can take that resource and pass it to a number of different functions. To retrieve a count of the rows, you can use mysql_num_rows()
, to retrieve the results of the query, you can use either mysql_fetch_array()
, mysql_fetch_assoc()
or mysql_fetch_object()
.
根据 上的文档mysql_query()
,SELECT 查询返回一个资源。您可以获取该资源并将其传递给许多不同的函数。要检索行数,您可以使用mysql_num_rows()
, 来检索查询结果,您可以使用mysql_fetch_array()
,mysql_fetch_assoc()
或mysql_fetch_object()
。
A normal pattern for dealing with database results will look something like this:
处理数据库结果的正常模式如下所示:
$result = mysql_query("SELECT * FROM persons"); // run query against database
$count = mysql_num_rows($result); // retrieve a count of the rows in the previous query
while ($row = mysql_fetch_assoc($result)) { // loop through all the rows in the resultset
// use $row['column_name'] to access columns in your resultset
}
From your example above:
从你上面的例子:
$result = mysql_query("SELECT COUNT(*) AS num FROM persons"); // run query against db
$row = mysql_fetch_assoc($result); // retrieve the 1 (and only) row
$count = $row['num']; // we needed to alias the COUNT(*) column as `num`
回答by mingos
mysql_query
returns a resource object. You need to fetch rows from it first (mysql_fetch_row
).
mysql_query
返回一个资源对象。您需要首先从中获取行 ( mysql_fetch_row
)。
回答by Valandres
Straight from PHP.net.......
直接来自 PHP.net ......
"For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error."
“对于 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回结果集的语句,mysql_query() 在成功时返回资源,在错误时返回 FALSE。”
回答by Joe Stefanelli
From the documentation on mysql_query:
从关于mysql_query的文档:
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
对于 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回结果集的语句,mysql_query() 在成功时返回资源,在错误时返回 FALSE。