PHP array problem - not looping through foreach

Thanks everyone, the question is answered! If anyone is interested, the updated function looks like this, all other codes remain the same:

function fetch_questions($page) {
global $link;
$proc = mysqli_prepare($link, "SELECT * FROM tquestions_cwh WHERE page = ?");
mysqli_stmt_bind_param($proc, "i", $page);
mysqli_stmt_execute($proc);


$rowq = array();
stmt_bind_assoc($proc, $rowq);

// loop through all result rows
//  while ($proc->fetch()) {
    //  print_r($rowq);
//      $rows[]=$rowq;
//  }

while ($proc->fetch())
{
    foreach($rowq as $key=>$value )
    {
        $row_tmb[ $key ] = $value;
    } 
    $rows[] = $row_tmb; 
}

mysqli_stmt_close($proc);
mysqli_clean_connection($link);
return($rows);

      

}

Okay,

Here is the code:

function fetch_questions($page) {
    global $link;
    $proc = mysqli_prepare($link, "SELECT * FROM tquestions_cwh WHERE page = ?");
    mysqli_stmt_bind_param($proc, "i", $page);
    mysqli_stmt_execute($proc);


    $rows = array();
    stmt_bind_assoc($proc, $rowq);

    // loop through all result rows
    while ($proc->fetch()) {
        //  print_r($rowq);
        $rows[]=$rowq;
    }

    mysqli_stmt_close($proc);
    mysqli_clean_connection($link);
    return($rows);
}

      

Then I add this to the php variable like:

$qs = fetch_questions($page);

      

Then I go through like this:

foreach($qs as $value){
                echo "<tr>".$value['qnum']." is the questions number and the question text is ".$value['qtext'].". The page and q values are ".$value['page']." and ".$value['questions']." respectively.</tr>";

      

The output, however, is this:

8 is the questions number and the question text is I know how what I do fits into my team objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team objectives. The page and q values are 1 and q8 respectively.8 is the questions number and the question text is I know how what I do fits into my team objectives. The page and q values are 1 and q8 respectively.

      

This is not what I want, for informational purposes the array using the print function looks like this:

Array
    (
        [0] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team objectives
                [page] => 1
            )

        [1] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team objectives
                [page] => 1
            )

        [2] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team objectives
                [page] => 1
            )

        [3] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team objectives
                [page] => 1
            )

        [4] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team objectives
                [page] => 1
            )

        [5] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team objectives
                [page] => 1
            )

        [6] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team objectives
                [page] => 1
            )

        [7] => Array
            (
                [questions] => q8
                [qnum] => 8
                [qtext] => I know how what I do fits into my team objectives
                [page] => 1
            )

    )

      

Clearly it doesn't iterate over and display every line as it should ... any advice?

Homer.

+2


a source to share


4 answers


I read the manual and found this might help you:

/*
while ($proc->fetch()) {         
    //  print_r($rowq);         
    $rows[]=$rowq;         
}
*/
while ($proc->fetch())
{
    foreach($rowq as $key=>$value )
    {
        $row_tmb[ $key ] = $value;
    } 
    $row[] = $row_tmb; 
}

      



quote: The problem is that what is returned $rowq

is a reference, not data. So when you write $row[] = $rowq

, $row

will be filled with the last element of the dataset.

+1


a source


Sorry I am not familiar with mysqli procedural style so I am not sure if this is your problem or not. However, it seems to me that you have set $ proc to the result of mysqli_prepare, but according to the manual , this function returns true or false. Then you used $ proc as the first parameter to mysqli_stmt_bind_param (), but again according to the manual , this parameter should be returned by mysqli_stmt_init (), which I assume is $ link. In fact, wherever you have used $ proc as a parameter, you should use $ link instead.



0


a source


Not sure if there is more, but the markup is wrong: you are missing open and close <td>

. The output you posted seems complete, it just appears on one line. Try:

foreach($qs as $value) {
    echo "<tr><td>".$value['qnum']." is the questions number and the question text is ".$value['qtext'].". The page and q values are ".$value['page']." and ".$value['questions']." respectively.</td></tr>";
}

      

0


a source


The mysqli_stmt_fetch manual says:

Note. Please note that all columns must be bound by the application before calling mysqli_stmt_fetch ().

Perhaps SELECT *

you should use functions to query mysqli_stmt_store_result

and mysqli_stmt_result_metadata

get all columns.

0


a source







All Articles