Warning: mysqli_fetch_assoc() expects parameter 1

2019-01-27 04:02发布

问题:

This question already has an answer here:

  • mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result 31 answers

I've looked over all the other questions pertaining to this 'warning' but none are the same as my issue. My issue seems to be quit simple though i just can't seem to figure it out. I've tried every solution i've seen on google and here but i'm not familiar enough with php/mysqli to resolve it. Thanks in advance!

<?php
$db = new mysqli('localhost', 'root', '', 'game');

if($db->connect_errno > 0){
    die('Unable to connect [' . $db->connect_errno . ']');
}

$query = mysqli_query($db, "SELECT * FROM `characters`");

$query_result = array();

while ($row = mysqli_fetch_assoc($query)) {
    $query_result[$row['id']] = array(

        'name' => $row['name'],
        'name' => $row['race'],
        'name' => $row['level']
    );
}

?>

回答1:

This happens usually when the mysqli_query hasnt run properly. Check whether $query is executed properly or does it contains anything or not. The problem may be with the connection or it may be with the query or it may be with the tables. Be sure $query contains the results.

Try this

if (!$query) {
        echo 'MySQL Error: ' . mysqli_error();
        exit;
    }

Let us know what is the mysql error



标签: php mysqli