Abel1416 Posted July 14, 2021 Share Posted July 14, 2021 I have only one result being displayed in a select query result whereas I'm expecting 4 results in an array. This is my code below. $sq2="SELECT course FROM course_reg WHERE userid=?"; $stm =$conn->prepare($sq2); $stm->bind_param("s",$logged); $stm->execute (); $return2= $stm->get_result(); $r2 = $return2->fetch_all(); //print_r($r2); foreach($r2 as $course){ foreach($course as $courses){ echo $courses; } } If I do print_r($r2); it comes out with array containing all the possible results. I.e Array ( [0] => Array ( [0] => CME211 ) [1] => Array ( [0] => CME511 ) [2] => Array ( [0] => CME311 ) [3] => Array ( [0] => CME411 ) ) When i loop through the array to get individual result, it only comes out with a single result. I.e CME211 I would be glad if you can help me figure where the issue is. Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/313390-why-am-i-not-getting-the-number-of-expected-result-through-the-loop/ Share on other sites More sharing options...
gw1500se Posted July 14, 2021 Share Posted July 14, 2021 Its possible your loop itself is failing after the first pass. Make sure you have error reporting on: error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/313390-why-am-i-not-getting-the-number-of-expected-result-through-the-loop/#findComment-1588346 Share on other sites More sharing options...
Barand Posted July 14, 2021 Share Posted July 14, 2021 Your code worked fine for me. Are you sure that, when you ran it, your logged user has more than 1 course? Quote Link to comment https://forums.phpfreaks.com/topic/313390-why-am-i-not-getting-the-number-of-expected-result-through-the-loop/#findComment-1588347 Share on other sites More sharing options...
Abel1416 Posted July 14, 2021 Author Share Posted July 14, 2021 (edited) @Barand I have looked at my table and there are multiple courses for the particular user. @gw1500se error reporting is on. From print_r($r2); all was fetched. Edited July 14, 2021 by Abel1416 Quote Link to comment https://forums.phpfreaks.com/topic/313390-why-am-i-not-getting-the-number-of-expected-result-through-the-loop/#findComment-1588349 Share on other sites More sharing options...
Barand Posted July 14, 2021 Share Posted July 14, 2021 This is the code I ran (of course using my similar table instead of yours) $sq2 = "SELECT pg_code FROM first_list WHERE st_ic = ?"; $stm = $conn->prepare($sq2); $stm->bind_param('s', $logged); $stm->execute (); $return2= $stm->get_result(); $r2 = $return2->fetch_all(); echo '<pre>' . print_r($r2, 1) . '</pre>'; foreach($r2 as $course){ foreach($course as $courses){ echo $courses.'<br>'; } } and this was the output Array ( [0] => Array ( [0] => DMM ) [1] => Array ( [0] => DKC ) [2] => Array ( [0] => DCS ) [3] => Array ( [0] => DPS ) ) DMM DKC DCS DPS FYI, here is a PDO version to give the same list $stm = $pdo->prepare("SELECT pg_code FROM first_list WHERE st_ic = ?"); $stm->execute([$logged]); foreach ($stm as $course) echo $course['pg_code'] . '<br>'; Quote Link to comment https://forums.phpfreaks.com/topic/313390-why-am-i-not-getting-the-number-of-expected-result-through-the-loop/#findComment-1588350 Share on other sites More sharing options...
mac_gyver Posted July 14, 2021 Share Posted July 14, 2021 what does the 'view source' of the output show? what do you get if you use var_dump() instead of print_r()? your posted code should display the correct result (though the 2nd foreach() isn't needed, just reference the zeroth element), based on the print_r() output you have shown. if it isn't, either that's not the whole code that's being executed (are you doing something like running another query inside of the loop), where you are outputting the values is preventing them from all being displayed where you are looking (inside of a html table w/o being part of the table markup), or there's something about the data values that's preventing them from being seen (some non-printing or html around the remaining values, that the print_r doesn't show.) Quote Link to comment https://forums.phpfreaks.com/topic/313390-why-am-i-not-getting-the-number-of-expected-result-through-the-loop/#findComment-1588351 Share on other sites More sharing options...
Psycho Posted July 14, 2021 Share Posted July 14, 2021 1 hour ago, mac_gyver said: the 2nd foreach() isn't needed, just reference the zeroth element) I would also highly recommend not referencing database results based on the numeric index and instead referencing results based on an associative array (i.e. named indexes based on the field names). It makes for more readable code and is much, much easier to maintain. The sample code by Barand uses such an implementation. Quote Link to comment https://forums.phpfreaks.com/topic/313390-why-am-i-not-getting-the-number-of-expected-result-through-the-loop/#findComment-1588352 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.