2010-04-24 81 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函數來獲取所有列。