mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource

后端 未结 30 3351
鱼传尺愫
鱼传尺愫 2020-11-21 06:25

I am trying to select data from a MySQL table, but I get one of the following error messages:

mysql_fetch_array() expects parameter 1 to be resource,

30条回答
  •  庸人自扰
    2020-11-21 07:17

    As scompt.com explained, the query might fail. Use this code the get the error of the query or the correct result:

    $username = $_POST['username'];
    $password = $_POST['password'];
    
    $result = mysql_query("
    SELECT * FROM Users 
    WHERE UserName LIKE '".mysql_real_escape_string($username)."'
    ");
    
    if($result)
    {
        while($row = mysql_fetch_array($result))
        {
            echo $row['FirstName'];
        }
    } else {
        echo 'Invalid query: ' . mysql_error() . "\n";
        echo 'Whole query: ' . $query; 
    }
    

    See the documentation for mysql_query() for further information.

    The actual error was the single quotes so that the variable $username was not parsed. But you should really use mysql_real_escape_string($username) to avoid SQL injections.

提交回复
热议问题