Call to a member function fetch_assoc ()
When encountering the above error, it means that your query failed, to know the source of the error check the line before the call fetch_assoc()
which is $mysqli->query($sql);
.
To get some hint of what the real problem was, see what the return of mysqli->error
is.
In your case, there are single quotes in the table name, which causes a syntax error, single quotes are only for values and not for escaping identifier names.
$sql = "SELECT * FROM 'usuarios'";
The error message returned is:
Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' table '' at line
That 1064 code is called SQL State, it identifies the category of the error and provides a valuable hint on how to solve the problem. 1064 means syntax error.
Code with error checking:
$query = $mysqli->query($sql) or die($mysqli->errno .' - '. $mysqli->error);
while ($dados = $query->fetch_assoc()) {
Or even
$query = $mysqli->query($sql);
if(!query){
echo 'erro: SQLState: '. $mysqli->errno .' - '. $mysqli->error;
exit;
}
Recommended reading:
Documentation - MySQLi-> error
A MySQL query with ciphers vs without
SQL State MySQL List