连接到 MySQL 数据库并计算行数
我需要连接到 MySQL 数据库,然后显示行数。这就是我到目前为止所得到的;
<?php
include "connect.php";
db_connect();
$result = mysql_query("SELECT * FROM hacker");
$num_rows = mysql_num_rows($result);
echo $num_rows;
?>
当我使用该代码时,我最终出现此错误;
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\username\Desktop\xammp\htdocs\news2\results.php on line 10
预先感谢:D
I need to connect to a MySQL database and then show the number of rows. This is what I've got so far;
<?php
include "connect.php";
db_connect();
$result = mysql_query("SELECT * FROM hacker");
$num_rows = mysql_num_rows($result);
echo $num_rows;
?>
When I use that code I end up with this error;
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\username\Desktop\xammp\htdocs\news2\results.php on line 10
Thanks in advance :D
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
养成以这种方式运行所有查询的习惯:
您将始终获得全面的错误信息
并采取适当的更正
,正如上面提到的,计算行数的唯一可靠方法是 SELECT count(*) 查询
take a habit to run all queries this way:
and you will always have comprehensive error information
and take appropriate corrections
also, as it was mentioned above, the only reliable way to count rows is
SELECT count(*)
query更改您的代码如下:
您有 SQL 错误或未连接到数据库
change your code as following:
You have an SQL-Error or your not connected to the database
您可能最好要求数据库聚合行数,而不是将它们全部传输到 php 并在那里进行计数。
You would probably be better of asking the database to aggregate the number of rows instead of transferring them all to php and doing the counting there.