[英]mysql_num_rows(): supplied argument is not a valid MySQL result resource
[英]mysql_num_rows(): supplied argument is not a valid MySQL result resource
如果选择得到一行,我需要得到真实的结果。 但是我什至无法回显行数。
我得到mysql_num_rows(): supplied argument is not a valid MySQL result resource
我该怎么做?
$result="SELECT * FROM Users WHERE User = '$user' AND Password = '$hash' LIMIT 1";
$num_rows = mysql_num_rows(resource $result);
echo $num_rows;
在尝试从执行中获取任何结果之前,必须使用mysql_query()
执行SQL查询。
因此,基本上,您的代码应如下所示:
// 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();
}
您必须先执行SQL查询,然后才能检索结果集中的行数:
$mysql_result = mysql_query($result); // Do this first, then you can
$num_rows = mysql_num_rows( $mysql_result);
这是一种更清洁的方法
$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'];
试试看 !!!
尝试:
$result = mysql_query("SELECT * FROM Users WHERE User = '$user' AND Password = '$hash' LIMIT 1");
另外,执行查询后,请检查是否有任何错误。 在这种情况下, $result
不是资源(实际上是FALSE
)。 要检索错误详细信息,请调用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());
这是你的责任,但是,这些错误处理例程不让它投入生产的代码,因为这会传播你的数据库的布局。 另外,检查插入查询中的变量是否正确转义(为此使用mysql_real_escape_string()
)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.