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

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

    Any time you get the...

    "Warning: mysqli_fetch_object() expects parameter 1 to be mysqli_result, boolean given"

    ...it is likely because there is an issue with your query. The prepare() or query() might return FALSE (a Boolean), but this generic failure message doesn't leave you much in the way of clues. How do you find out what is wrong with your query? You ask!

    First of all, make sure error reporting is turned on and visible: add these two lines to the top of your file(s) right after your opening tag:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    

    If your error reporting has been set in the php.ini you won't have to worry about this. Just make sure you handle errors gracefully and never reveal the true cause of any issues to your users. Revealing the true cause to the public can be a gold engraved invitation for those wanting to harm your sites and servers. If you do not want to send errors to the browser you can always monitor your web server error logs. Log locations will vary from server to server e.g., on Ubuntu the error log is typically located at /var/log/apache2/error.log. If you're examining error logs in a Linux environment you can use tail -f /path/to/log in a console window to see errors as they occur in real-time....or as you make them.

    Once you're squared away on standard error reporting adding error checking on your database connection and queries will give you much more detail about the problems going on. Have a look at this example where the column name is incorrect. First, the code which returns the generic fatal error message:

    $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
    $query = $mysqli->prepare($sql)); // assuming $mysqli is the connection
    $query->bind_param('s', $definition);
    $query->execute();
    

    The error is generic and not very helpful to you in solving what is going on.

    With a couple of more lines of code you can get very detailed information which you can use to solve the issue immediately. Check the prepare() statement for truthiness and if it is good you can proceed on to binding and executing.

    $sql = "SELECT `foo` FROM `weird_words` WHERE `definition` = ?";
    if($query = $mysqli->prepare($sql)) { // assuming $mysqli is the connection
        $query->bind_param('s', $definition);
        $query->execute();
        // any additional code you need would go here.
    } else {
        $error = $mysqli->errno . ' ' . $mysqli->error; // 1054 Unknown column 'foo' in 'field list'
        // handle error
    }
    

    If something is wrong you can spit out an error message which takes you directly to the issue. In this case, there is no foo column in the table, solving the problem is trivial.

    If you choose, you can include this checking in a function or class and extend it by handling the errors gracefully as mentioned previously.

提交回复
热议问题