如果select获得一行,我需要成功。但是我甚至无法回显行数。
我得到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;
答案 0 :(得分:1)
在尝试从执行中获取任何结果之前,必须使用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();
}
答案 1 :(得分:1)
您必须先执行SQL查询,然后才能检索结果集中的行数:
$mysql_result = mysql_query($result); // Do this first, then you can
$num_rows = mysql_num_rows( $mysql_result);
答案 2 :(得分:1)
这是一种更清洁的方式
$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'];
试一试!!!
答案 3 :(得分:0)
尝试:
$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()
)。