PHP数组问题 - 不是通过foreach循环

时间:2010-04-24 08:15:06

标签: php

谢谢大家,问题解答了!如果有人感兴趣,更新的功能如下,所有其他代码保持不变:

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

}

确定,

以下是代码:

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);
}
然后我将它添加到php变量中,如下所示:

$qs = fetch_questions($page);

然后我循环通过,就像这样:

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

然而输出是:

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.

这不是我想要的,出于提供信息的目的,使用print函数的数组如下所示:

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
            )

    )

显然,它没有循环显示每一行,因为它应该......任何建议?

荷马。

4 个答案:

答案 0 :(得分:1)

我看了manual并发现这可能会对您有所帮助:

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

引用:问题是返回的$rowq是引用而不是数据。因此,当您编写$row[] = $rowq时,$row将填充数据集的最后一个元素。

答案 1 :(得分:0)

抱歉,我对mysqli的程序风格并不熟悉,所以我不确定这是不是你的问题。但是,在我看来,你将$ proc设置为mysqli_prepare的结果,但according to the manual,此函数返回true或false。然后你使用$ proc作为mysqli_stmt_bind_param()的第一个参数,但是再次according to the manual,这个参数应该是mysqli_stmt_init()的返回值,我假设它是$ link。事实上,无论你使用$ proc作为参数,都应该使用$ link。

答案 2 :(得分:0)

不确定是否有更多,但标记错误:您错过了开始和结束<td>。您发布的输出似乎已完成,它只显示在一行中。尝试:

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

答案 3 :(得分:0)

mysqli_stmt_fetch手册说:

  

注意:请注意,在调用mysqli_stmt_fetch()之前,所有列都必须由应用程序绑定。

对于SELECT *查询,您应该使用mysqli_stmt_store_resultmysqli_stmt_result_metadata函数来获取所有列。