Call to a Member Function fetch_assoc()
When you come across the above error, it means that your query failed, to know the origin of the error check the previous line to call fetch_assoc()
that is $mysqli->query($sql);
.
to get some hint of what was the real problem see what is the return of mysqli->error
.
In your case, it has simple quotes in the table name, which causes a syntax error, simple quotes are only for values and not to escape 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 and it identifies what the error category is and provides a valuable tip on how to solve the problem. 1064 means syntax error.
Code with error check:
$query = $mysqli->query($sql) or die($mysqli->errno .' - '. $mysqli->error);
while ($dados = $query->fetch_assoc()) {
Or else
$query = $mysqli->query($sql);
if(!query){
echo 'erro: SQLState: '. $mysqli->errno .' - '. $mysqli->error;
exit;
}
Recommended reading:
Documentation - Mysqli->error
A Mysql query, with crases vs without
List of SQL State Mysql
Your query has an error, it will not quote simple in table name.
– rray
The question was edited and approved but the formatting was not applied. Galera ta approving on automatic, without checking if the edition actually improves the reading of the question.
– user28595
Thank you very much, I’m picking up a bit with mysqli, so far I haven’t been able to make a login system with session with it. More I’m looking for, VLW
– Fernando barboza da silva
Could you mark the answer that solved the problem as accepted? is a green light. See How and why to accept an answer?
– rray