I have had a search around on this but the answers I could find only provide more detailed error reports - I know why the error exists I am trying to prevent the error happening!
My code is:
$player_sql = "SELECT player FROM player_instruments WHERE instrument = '".$instrument_id."'" . $users_helping;
$result = $db->query($player_sql) or trigger_error($db->error);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$position_where .= $row['player'] .",";
}
Now when there are results this problem produces no error, but with no results from the query I receive the error:
PHP Notice: Trying to get property of non-object
This is because the statement $result->num_rows has no results, unfortunately this is what this line of code is supposed to be checking!
So long and short of it, how do I verify if results exists without this error coming up when there aren't any results?
</div>
Check if $result is an object
if (is_object($result) && $result->num_rows > 0) {
Spent an hour or two trying to figure this one out, and on many different sites.
I will lay it out in practical terms. There are 2 ways to json_decode. Depending on which you use changes how you should respond to it later in code.
1: $Response_decode1 = json_decode($response1, TRUE);
2: $Response_decode2 = json_decode($response2);
Option 1 makes the response $Response_decode1 act like a native PHP array callable by name. eg use square brackets (in PHP) when calling children.
1: $Response_decode1['level1']['level2']
Option 2 makes the response $Response_decode2 act like a responding object with (children) objects called by name
2: $Response_decode2->level1->level2
EG Type 1
foreach ($Response_decode1['level1']['level2']['level3'] as $entry){
$TelNo .= $entry['value'];
$TelNo .= " / ";
}
EG Type 2
foreach ($Response_decode2->level1->level2->level3 as $entry){
$TelNo .= $entry->value;
$TelNo .= " / ";
}
I trust this helps