简体   繁体   English

mysql_num_rows():提供的参数不是有效的MySQL结果资源

[英]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.

相关问题 mysql_num_rows():提供的参数不是有效的MySQL结果资源 - mysql_num_rows(): supplied argument is not a valid MySQL result resource 警告mysql_num_rows():提供的参数不是有效的MySQL结果 - Warning mysql_num_rows(): supplied argument is not a valid MySQL result 警告:mysql_num_rows():提供的参数不是没有mysql_error()的有效MySQL结果资源 - Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource with no mysql_error() 警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源 - Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource 如何解决mysql_num_rows():提供的参数不是PHP中的有效MySQL结果资源 - How to solve mysql_num_rows(): supplied argument is not a valid MySQL result resource in php 警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源 - Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource 警告:mysql_num_rows():提供的参数不是有效的MySQL结果资源nationalbuilder webhooks API - Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource nationbuilder webhooks API mysql_num_rows():提供的参数在15中不是有效的MySQL结果资源 - mysql_num_rows(): supplied argument is not a valid MySQL result resource in 15 mysql_num_rows():提供的参数不是有效的MySQL结果资源(唯一) - mysql_num_rows(): supplied argument is not a valid MySQL result resource (unique) 修复错误:mysql_num_rows():提供的参数不是有效的MySQL结果资源 - Fixing the error: mysql_num_rows(): supplied argument is not a valid MySQL result resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM