为什么返回资源ID#2? [英] Why does this return Resource id #2?

查看:48
本文介绍了为什么返回资源ID#2?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
从PHP的MySql响应中回显资源ID#6"?

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

我是php和SQL的新手,我正在尝试使php页面列出表中的枚举数. 我正在使用此代码,但它返回资源ID#2:

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:

$rt=mysql_query("SELECT COUNT(*) FROM persons");
echo mysql_error();
echo "<h1>Number:</h1>".$rt;

推荐答案

因为执行使用类似 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);
}

这篇关于为什么返回资源ID#2?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆