[英]mysql_num_rows(): supplied argument is not a valid MySQL result resource
I need to get true if the select gets one row. 如果选择得到一行,我需要得到真实的结果。 However I cannot even echo the number of rows.
但是我什至无法回显行数。
I get mysql_num_rows(): supplied argument is not a valid MySQL result resource
我得到
mysql_num_rows(): supplied argument is not a valid MySQL result resource
How can I do it? 我该怎么做?
$result="SELECT * FROM Users WHERE User = '$user' AND Password = '$hash' LIMIT 1";
$num_rows = mysql_num_rows(resource $result);
echo $num_rows;
You must execute your SQL query, with mysql_query()
, before trying to get any result from that execution. 在尝试从执行中获取任何结果之前,必须使用
mysql_query()
执行SQL查询。
So, basically, your code should look like this : 因此,基本上,您的代码应如下所示:
// This is a QUERY, not a RESULT
$query = "SELECT * FROM Users WHERE User = '$user' AND Password = '$hash' LIMIT 1";
$result = mysql_query($query); // execute query => get a resource
if ($result) {
// query has been executed without error
$num_rows = mysql_num_rows($result); // Use the result of the query's execution
echo $num_rows;
}
else {
// There has been an error ; deal with it
// for debugging, displaying the error message is OK :
echo mysql_error();
}
You have to execute the SQL query first, then you can retrieve the number of rows in the result set: 您必须先执行SQL查询,然后才能检索结果集中的行数:
$mysql_result = mysql_query($result); // Do this first, then you can
$num_rows = mysql_num_rows( $mysql_result);
Here is a cleaner way 这是一种更清洁的方法
$st = "SELECT COUNT(1) FoundCount FROM Users WHERE User='$user' AND Password='$hash'";
$result = mysql_query( $st );
$row = mysql_fetch_assoc( $result );
$rowfound = $row['FoundCount'];
Give it a Try !!! 试试看 !!!
Try: 尝试:
$result = mysql_query("SELECT * FROM Users WHERE User = '$user' AND Password = '$hash' LIMIT 1");
Also, after executing the query, check if there were any errors. 另外,执行查询后,请检查是否有任何错误。
$result
is not a resource in that case (it's FALSE
, actually). 在这种情况下,
$result
不是资源(实际上是FALSE
)。 To retrieve error details, call mysql_error()
: 要检索错误详细信息,请调用
mysql_error()
:
$query = "SELECT * FROM Users WHERE User = '$user' AND Password = '$hash' LIMIT 1";
$result = mysql_query($query);
if ($result === FALSE)
die("Error in query $query: " . mysql_error());
It is your responsibility, however, that these error handling routines do not make it into production code, as this would propagate your database's layout. 这是你的责任,但是,这些错误处理例程不让它投入生产的代码,因为这会传播你的数据库的布局。 Also, check if variables interpolated into a query are properly escaped (use
mysql_real_escape_string()
for that). 另外,检查插入查询中的变量是否正确转义(为此使用
mysql_real_escape_string()
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.