Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP数组问题-未通过foreach循环_Php - Fatal编程技术网

PHP数组问题-未通过foreach循环

PHP数组问题-未通过foreach循环,php,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 = arr

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

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>";
然后我循环了一下,就像这样:

$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>";
这不是我想要的,出于信息目的,使用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
            )

    )
很明显,它没有像应该的那样循环显示每一行……有什么建议吗


荷马。

对不起,我不太熟悉mysqli的程序风格,所以我不确定这是否是您的问题。然而,在我看来,您已经将$proc设置为mysqli_prepare的结果,但是,这个函数返回true或false。然后,您使用$proc作为mysqli_stmt_bind_param()的第一个参数,但是,这个参数应该是mysqli_stmt_init()的返回值,我假设它是$link。事实上,无论您在哪里使用$proc作为参数,都应该使用$link。

不确定是否还有更多,但标记是错误的:您缺少打开和关闭
。您发布的输出似乎已完成,它只显示在一行中。尝试:

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>";
}
foreach($qs作为$value){
回显“$value['qnum']”是问题编号,问题文本是“$value['qtext']”。页面和q值分别是“$value['page']”和“$value['questions']”;
}

mysqli\u stmt\u fetch手册上说:

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

对于
SELECT*
查询,您应该使用
mysqli\u stmt\u store\u result
mysqli\u stmt\u result\u metadata
函数来获取所有列。

我阅读了

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

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

数据库中的实际8个条目是什么?好奇的是,您能否使用:echo var_export($qs,true)添加$qs数组的转储?请提供您的需要clearly@SpawnCxy-谢谢!我对你的代码做了一点小小的修改,阅读了上面编辑的问题。