-2

i keep getting this error: Fatal error: Call to a member function fetch_assoc() on a non-object

and non of the other answers for this issue you will solve my issue. any idea what the problem is here?

<?
$link = mysqli_connect("localhost","","","mysql");

$data = 'SELECT * FROM `league_quiz` ORDER BY score';
$result = $link->query($data);
while( $row = $result->fetch_assoc()){
echo $row['leaguename'] . " " . $row['quizscore'];
echo "<br/>";

}
$link->close ();
?>
  • Are you sure those username and password "" aren't blank? – Ali Gajani Dec 27 '13 at 02:17
  • I've rarely seen a mysql user who has no username. I would then believe that is why your connection is returning false. – Daedalus Dec 27 '13 at 02:19
  • they are not blank! i just made them in here, i'm sure they are correct. – user3130214 Dec 27 '13 at 02:19
  • @user3130214 And we'd know that, how? I mean, if there was something there, the least you could do is let us know by maybe filling it with asterisks. Secondly, you don't check for any errors, aside from the parsing bit.. So debug some. – Daedalus Dec 27 '13 at 02:20

1 Answers1

1

The first step of solving any problem is to understand it.

Fatal error: Call to a member function fetch_assoc() on a non-object

In simpler language: you're trying to use the method fetch_assoc() on something that isn't an object, specifically $result. So there's your problem: $result isn't an object.

So how may this be? You assign it here

$result = $link->query($data);

So that means your query must be failing. To see the error, you can try to print mysqli_error($link).

And in the future, always check whether your commands execute succesfully, e.g.

if (mysqli_connect_errno())
  printf("Error during connection: %s\n", mysqli_connect_error());
  return false;
}

And later

if (!$result) {
  printf("Error during querying: %s\n", mysqli_error($link));
  return false;
}

Or something similar, depending on your exact code.

kba
  • 19,333
  • 5
  • 62
  • 89