2

我需要连接到 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

先谢谢了

4

3 回答 3

5

您可能会更好地要求数据库汇总行数,而不是将它们全部传输到 php 并在那里进行计数。

SELECT COUNT(*) FROM hacker
于 2010-06-17T13:15:31.793 回答
2

养成以这种方式运行所有查询的习惯:

$sql   = "SELECT * FROM hacker";
$res   = mysql_query($query) or trigger_error(mysql_error().$sql);

您将始终获得全面的错误信息
并采取适当的更正

此外,正如上面提到的,计算行数的唯一可靠方法是SELECT count(*)查询

$sql   = "SELECT count(*) FROM hacker";
$res   = mysql_query($query) or trigger_error(mysql_error().$sql);
$row   = mysql_fetch_row($res);
$count = $row[0];
于 2010-06-17T13:25:15.450 回答
1

change your code as following:

$result = mysql_query("SELECT * FROM hacker");
echo mysql_error();

You have an SQL-Error or your not connected to the database

于 2010-06-17T13:18:03.473 回答