Jump to content

Why am I not getting the number of expected result through the loop?


Abel1416

Recommended Posts

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!!!

 

 

Link to comment
Share on other sites

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>';

 

Link to comment
Share on other sites

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.)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.