致命错误:当我尝试运行我的php时,调用非对象上的成员函数fetch_assoc()

Wil*_*lli 0 php database

我创建了一个包含两个表'user'和'volley'的数据库.这个PHP代码帮助我连接这些表并检索数据.

我收到这个错误

致命错误:在第11行的/home/u115908902/public_html/Wishes.php中调用非对象上的成员函数fetch_assoc()

而我试图运行我的PHP代码.

<?php 
$mysqli = NEW MySQLi("mysql.hostinger.in","willi","123","pract"); 

global $mysqli;
$resultSet = $mysqli->query("SELECT user.name AS userName,
volley.wishes AS userWishes
FROM user,volley 
WHERE user.name = 'mick' AND volley.id=user.id");

$resultSet = array();
while($rows = $resultSet->fetch_assoc()){
array_push($resultSet,
array(
$users = $rows['userName'],
$volleys = $rows['userWishes']));
echo json_encode(array("resultSet"=>$resultSet));
mysqli_close($con);
}
?>
Run Code Online (Sandbox Code Playgroud)

Rig*_*lly 5

您正在使用该行销毁结果集 $resultSet = array();

此外,您正在卸载结果集过于复杂,它不需要

<?php 
$mysqli = NEW MySQLi("mysql.hostinger.in","willi","123","pract"); 

// remove this line its nonsence    
//global $mysqli;

$resultSet = $mysqli->query("SELECT user.name AS userName,
                                    volley.wishes AS userWishes
                             FROM user,volley 
                             WHERE user.name = 'mick' AND volley.id=user.id");

// remove this line it destroys your resultset    
//$resultSet = array();

$results = array();

while($row = $resultSet->fetch_assoc()){
    $results[] = $row;
}

// this does not belong in the loop
mysqli_close($mysqli);
// nor does this
echo json_encode($results);
?>
Run Code Online (Sandbox Code Playgroud)