2010-04-24 78 views
2

谢谢大家,问题已回答!如果有人有兴趣,更新的功能如下,所有其他代码保持不变:PHP Array问题 - 无法通过foreach循环使用

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. 

这不是我想要的东西,以供参考,使用打印功能看起来数组像这样:

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 
      ) 

    ) 

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

荷马。

+0

数据库中那些实际的8个条目是什么? – macbirdie 2010-04-24 08:31:28

+0

好奇,请问您可以使用以下命令添加$ qs数组的转储:echo var_export($ qs,true)? – Cesar 2010-04-24 08:34:35

+0

请提供您的需求清楚 – Karthik 2010-04-24 08:39:40

回答

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将填充数据集的最后一个元素。

+0

@SpawnCxy - 谢谢!我在编辑的问题中对您的代码进行了轻微修改,以便在上面进行阅读。 – 2010-04-24 13:21:40

+0

@ Homer_J,不客气。玩得开心:) – Young 2010-04-24 14:22:28

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。

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

mysqli_stmt_fetch手册说:

注:请注意,所有列必须由应用程序调用mysqli_stmt_fetch之前的约束()。

也许为SELECT *查询,你应该使用mysqli_stmt_store_resultmysqli_stmt_result_metadata函数来获取所有列。