Note that there are some explanatory texts on larger screens.

plurals
  1. POCall to a member function fetch_assoc() on a non-object - pretty sure sql query is correct
    primarykey
    data
    text
    <p>I've searched on SO already for an answer to my problem, without any luck. As far as I can tell, there should be no problems with my sql query:</p> <pre><code>SELECT * FROM scores WHERE `primary_id`=$id; </code></pre> <p>I definitely have a table called "scores" with a column called "primary_id", and I can perform inserts, updates etc on it with no problems. However whenever I try to perform a select, I get the "Call to a member function fetch_assoc() on a non-object" error. All of the posts I've seen suggest it is caused by the sql query failing and returning false. The odd thing is, that even though the while ( $row = $result->fetch_assoc() ) throws an error, it still performs the update query contained inside. Maybe that's not odd to someone more experienced, but to me if the "while" call fails I wouldn't expect the contents to be executed, especially seeing as it somehow still gets the fields from $row.</p> <p>Here is the code.</p> <pre><code>date_default_timezone_set('UTC'); $currentWeek = date("W"); $currentMonth = date("m"); $sql = "SELECT * FROM scores WHERE primary_id=$id;"; $result = $this-&gt;db-&gt;query($sql) or trigger_error(mysql_error().$sql); if (!$result) { die('Something went wrong: '.$mysqli-&gt;error()); } else { echo "result exists";//this successfully echoes, so $result must exist } while ( $row = $result-&gt;fetch_assoc() ){ $week = $row['scoreweek']; $month = $row['scoremonth']; if($row['weekNumber'] != $currentWeek){ $week = 0; } if($row['monthNumber'] != $currentMonth){ $month = 0; } $sql = "UPDATE scores SET `weekNumber` = $currentWeek, `monthNumber` = $currentMonth, `scoreweek` = $week + $score, `scoremonth` = $month + $score, `scoreall` = `scoreall` + $score WHERE `primary_id` = $id;"; $result = $this-&gt;db-&gt;query($sql); } </code></pre> <p>Have I missed something obvious? I've tried the statement with and without apostrophes in various places, but with no luck.</p> <p>Edit: calling var_dump($result) gives me the following:</p> <pre><code>object(mysqli_result)#3 (5) { ["current_field"]=&gt; int(0) ["field_count"]=&gt; int(8) ["lengths"]=&gt; NULL ["num_rows"]=&gt; int(1) ["type"]=&gt; int(0) } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload