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

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

    Error occurred here was due to the use of single quotes ('). You can put your query like this:

    mysql_query("
    SELECT * FROM Users 
    WHERE UserName 
    LIKE '".mysql_real_escape_string ($username)."'
    ");
    

    It's using mysql_real_escape_string for prevention of SQL injection. Though we should use MySQLi or PDO_MYSQL extension for upgraded version of PHP (PHP 5.5.0 and later), but for older versions mysql_real_escape_string will do the trick.

    0 讨论(0)
  • 2020-11-21 07:13
    $query = "SELECT Name,Mobile,Website,Rating FROM grand_table order by 4";
    
    while( $data = mysql_fetch_array($query))
    {
        echo("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td></tr>");      
    }
    

    Instead of using a WHERE query, you can use this ORDER BY query. It's far better than this for use of a query.

    I have done this query and am getting no errors like parameter or boolean.

    0 讨论(0)
  • 2020-11-21 07:13

    There might be two reasons:

    1. Have you opened the connection to the database prior to calling mysql_query function? I don't see that in your code. Use mysql_connect before making the query. See php.net/manual/en/function.mysql-connect.php

    2. The variable $username is used inside a single quote string, so its value will not be evaluated inside the query. The query will definitely fail.

    Thirdly, the structure of query is prone to SQL injection. You may use prepared statements to avoid this security threat.

    0 讨论(0)
  • 2020-11-21 07:13

    If you don't have any MySQL Error appearing while checking, make sure that you properly created your database table. This happened to me. Look for any unwanted commas or quotes.

    0 讨论(0)
  • 2020-11-21 07:15

    A query may fail for various reasons in which case both the mysql_* and the mysqli extension will return false from their respective query functions/methods. You need to test for that error condition and handle it accordingly.

    mysql_* extension:

    NOTE The mysql_ functions are deprecated and have been removed in php version 7.

    Check $result before passing it to mysql_fetch_array. You'll find that it's false because the query failed. See the mysql_query documentation for possible return values and suggestions for how to deal with them.

    $username = mysql_real_escape_string($_POST['username']);
    $password = $_POST['password'];
    $result = mysql_query("SELECT * FROM Users WHERE UserName LIKE '$username'");
    
    if($result === FALSE) { 
        die(mysql_error()); // TODO: better error handling
    }
    
    while($row = mysql_fetch_array($result))
    {
        echo $row['FirstName'];
    }
    

    mysqli extension
    procedural style:

    $username = mysqli_real_escape_string($mysqli, $_POST['username']);
    $result = mysqli_query($mysqli, "SELECT * FROM Users WHERE UserName LIKE '$username'");
    
    // mysqli_query returns false if something went wrong with the query
    if($result === FALSE) { 
        yourErrorHandler(mysqli_error($mysqli));
    }
    else {
        // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach
        foreach( $result as $row ) {
            ...
    

    oo-style:

    $username = $mysqli->escape_string($_POST['username']);
    $result = $mysqli->query("SELECT * FROM Users WHERE UserName LIKE '$username'");
    
    if($result === FALSE) { 
        yourErrorHandler($mysqli->error); // or $mysqli->error_list
    }
    else {
        // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach
        foreach( $result as $row ) {
          ...
    

    using a prepared statement:

    $stmt = $mysqli->prepare('SELECT * FROM Users WHERE UserName LIKE ?');
    if ( !$stmt ) {
        yourErrorHandler($mysqli->error); // or $mysqli->error_list
    }
    else if ( !$stmt->bind_param('s', $_POST['username']) ) {
        yourErrorHandler($stmt->error); // or $stmt->error_list
    }
    else if ( !$stmt->execute() ) {
        yourErrorHandler($stmt->error); // or $stmt->error_list
    }
    else {
        $result = $stmt->get_result();
        // as of php 5.4 mysqli_result implements Traversable, so you can use it with foreach
        foreach( $result as $row ) {
          ...
    

    These examples only illustrate what should be done (error handling), not how to do it. Production code shouldn't use or die when outputting HTML, else it will (at the very least) generate invalid HTML. Also, database error messages shouldn't be displayed to non-admin users, as it discloses too much information.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题