PHP Array issue - not looping through foreach...

Posted by Homer_J on Stack Overflow See other posts from Stack Overflow or by Homer_J
Published on 2010-04-24T08:15:05Z Indexed on 2010/04/24 8:23 UTC
Read the original article Hit count: 149

Filed under:

Ok,

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);
}

I then add this to a php variable, like so:

$qs = fetch_questions($page);

I then loop through is, like so:

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's 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's 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's 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's 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's 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's 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's 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's objectives. The page and q values are 1 and q8 respectively.

Which is not what I want, for information 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's objectives
                [page] => 1
            )

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

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

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

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

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

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

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

    )

Clearly it's not looping through and displaying each row as it should...any advice?

Homer.

© Stack Overflow or respective owner

Related posts about php