简体   繁体   English

帮助 我在使用 PHP 时收到一条错误消息“警告:mysql_num_rows():提供的参数不是有效的 MySQL 结果资源...”

[英]Help I have an error message using PHP “Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in…”

I'm trying to run a query in MySql database but I have the same warning everytime.我正在尝试在 MySql 数据库中运行查询,但每次都收到相同的警告。 Can someone help.有人可以帮忙吗。

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/public_html/login.php on line 279 The Username you supplied does not exist!警告:mysql_num_rows():在第 279 行提供的参数不是有效的 MySQL 结果资源/XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX/public_html/login.php 您提供的用户名不存在!

I have highlighted line 279 with ++++++++++++$num = mysql_num_rows($res);++++++++++++我用 ++++++++++++$num = mysql_num_rows($res);++++++++++++ 突出显示了第 279 行

Please let me know how I can rectify the problem.请让我知道如何解决这个问题。

<?php
  //If the user has submitted the form
  if($_POST['submit']) {
    //protect the posted value then store them to variables
    $username = protect($_POST['username']);
    $password = protect($_POST['password']);

    //Check if the username or password boxes were not filled in
    if(!$username || !$password) {
      //if not display an error message
      echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
    } else {
      //if the were continue checking

      //select all rows from the table where the username matches the one entered by the user
      $res = mysql_query("SELECT * 
                            FROM `users` 
                           WHERE `username` = '".$username."'");
      ++++++++++++++$num = mysql_num_rows($res);++++++++++++++++++

      //check if there was not a match
      if($num == 0) {
        //if not display an error message
        echo "<center>The <b>Username</b> you supplied does not exist!</center>";
      } else {
        //if there was a match continue checking

        //select all rows where the username and password match the ones submitted by the user
        $res = mysql_query("SELECT * FROM `users` 
                             WHERE `username` = '".$username."' 
                               AND `password` = '".$password."'");
        $num = mysql_num_rows($res);

        //check if there was not a match
        if($num == 0) {
          //if not display error message
          echo "<center>The <b>Password</b> you supplied does not match the one for that username!</center>";
        } else {
                    //if there was continue checking

                    //split all fields fom the correct row into an associative array
                    $row = mysql_fetch_assoc($res);

                    //check to see if the user has not activated their account yet
                    if($row['active'] != 1){
                        //if not display error message
                        echo "<center>You have not yet <b>Activated</b> your account!</center>";
                    }else{
                        //if they have log them in

                        //set the login session storing there id - we use this to see if they are logged in or not
                        $_SESSION['uid'] = $row['id'];
                        //show message
                        echo "<center>You have successfully logged in!</center>";

                        //update the online field to 50 seconds into the future
                        $time = date('U')+50;
                        mysql_query("UPDATE `users` SET `online` = '".$time."' WHERE `id` = '".$_SESSION['uid']."'");

                        //redirect them to the usersonline page
                        header('Location: usersOnline.php');
                    }
                }
            }
        }
    }

    ?>

I am not sure what the protect function is doing but here is what you can do:我不确定保护 function 正在做什么,但您可以执行以下操作:

$username = mysql_real_escape_string($_POST['username']);
$password = mysql_real_escape_string($_POST['password']);

and in your query, for debugging use this to get the correct error并在您的查询中,用于调试使用它来获得正确的错误

$res = mysql_query("SELECT * 
                  FROM `users` 
                  WHERE 
                     `username` = '$username' 
                  AND 
                     `password` = '$password'") or die('Error: '.mysql_error());

It is possible that $username contains special characters? $username 可能包含特殊字符吗? you should check in the mysql log if your query is executed properly from the database point of view.如果您的查询从数据库的角度正确执行,您应该检查 mysql 日志。

Probably your query fails.可能您的查询失败。 mysql_query() returns false , if an error occurs.如果发生错误, mysql_query()返回false Use mysql_error() to identify the real problem.使用mysql_error()来确定真正的问题。

$res = mysql_query("SELECT * FROM `users` WHERE `username` = '".$username."'");
if ($res === false) {
    echo mysql_errno() . ': ' . mysql_error();
    exit;
}
$num = mysql_num_rows($res);

As the documentation of mysql_query() states:正如 mysql_query() 的文档所述:

For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.对于 SELECT、SHOW、DESCRIBE、EXPLAIN 和其他返回结果集的语句,mysql_query() 成功时返回资源,错误时返回 FALSE。

So just check, whether you have within the connected database this table, listed columns and your query is ok - it may be just the error of your query.因此,只需检查您在连接的数据库中是否有此表、列出的列以及您的查询是否正常 - 这可能只是您的查询错误。

There is the example within the documentation (see the link at the beginning), how to see what actually happened (see Example #1; replace with your own query):文档中有示例(请参阅开头的链接),如何查看实际发生的情况(请参阅示例 #1;替换为您自己的查询):

$result = mysql_query('SELECT * WHERE 1=1');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告: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 mysql_num_rows():提供的参数不是有效的MySQL结果资源 - mysql_num_rows(): supplied argument is not a valid MySQL result resource 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 resource 警告: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():提供的参数不是有效的 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结果资源 - Fixing the error: mysql_num_rows(): supplied argument is not a valid MySQL result resource
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM