0

Below query works fine. But how can I select the 3 results separately with the array_number?

I need to separate the 3 results correct with help from the array_number. But do not know how.

$records = $wpdb->get_results("
SELECT * FROM wp_rdp_participants_answers AS answ 
INNER JOIN wp_rdp_game_images AS game ON game.image_id = answ.image_id
WHERE answ.game_id = $i 
&& answ.answer_time LIKE '$day%' 
AND answ.answer = game.correct_answer
ORDER BY answ.answer_time ASC 
LIMIT 3", ARRAY_A);

if(count($records) > 0){
    foreach($records as $rec){      

    echo $rec->user_id.' - '. $rec->answer_time.' - '.$rec->answer.' - '. $rec->correct_answer. '<br>' ;
    }
}

How do I get now array[0] , array[1], array[2] ?

0

Errm...

$item_1 = $records[0];
$item_2 = $records[1];
$item_3 = $records[2];

// e.g.
echo $item_2->answer_time;
4
  • $item_0 = $records[0]; $item_1 = $records[1]; $item_2 = $records[2]; echo $item_0->user_email.' <br>' ; echo $item_1->user_email.' <br>' ; echo $item_2->user_email.' <br>' ; results in: Notice: Undefined offset: 0 in .. on line 15 Notice: Undefined offset: 1 in ... on line 16 Notice: Undefined offset: 2 in .. on line 17 Notice: Trying to get property of non-object in /home/deb96951n2/domains/xxxxxx.nl/public_html/rdp/wp-content/plugins/rdp-plugin/includes/test.php on line 18 – Hermants Oct 28 '16 at 15:50
  • Remove , ARRAY_A from your original code. – TheDeadMedic Oct 28 '16 at 15:51
  • Still getting errors: Notice: Undefined offset: 1 in xxx on line 18 Notice: Undefined offset: 2 in xxx on line 19 Notice: Trying to get property of non-object in xxx on line 21 Notice: Trying to get property of non-object in xxx on line 21 Notice: Trying to get property of non-object in xxx on line 22 Notice: Trying to get property of non-object in xxx on line 22 – Hermants Oct 28 '16 at 16:37
  • Because the result set is empty - my code was just to demonstrate the concept. You should still check count( $records ) – TheDeadMedic Oct 28 '16 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.