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

后端 未结 30 3281
鱼传尺愫
鱼传尺愫 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:16

    $result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');
    

    You define the string using single quotes and PHP does not parse single quote delimited strings. In order to obtain variable interpolation you will need to use double quotes OR string concatenation (or a combination there of). See http://php.net/manual/en/language.types.string.php for more information.

    Also you should check that mysql_query returned a valid result resource, otherwise fetch_*, num_rows, etc will not work on the result as is not a result! IE:

    $username = $_POST['username'];
    $password = $_POST['password'];
    $result = mysql_query('SELECT * FROM Users WHERE UserName LIKE $username');
    
    if( $result === FALSE ) {
       trigger_error('Query failed returning error: '. mysql_error(),E_USER_ERROR);
    } else {
       while( $row = mysql_fetch_array($result) ) {
          echo $row['username'];
       }
    }
    

    http://us.php.net/manual/en/function.mysql-query.php for more information.

提交回复
热议问题