Warning: mysqli_fetch_assoc() expects parameter 1

2019-01-27 04:26发布

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']
    );
}

?>

标签: php mysqli
1条回答
做自己的国王
2楼-- · 2019-01-27 05:05

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

查看更多
登录 后发表回答