謝謝大家,問題已回答!如果有人有興趣,更新的功能如下,所有其他代碼保持不變: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
)
)
很明顯,它沒有循環顯示每一行,因爲它應該...任何建議?
荷馬。
數據庫中那些實際的8個條目是什麼? – macbirdie 2010-04-24 08:31:28
好奇,請問您可以使用以下命令添加$ qs數組的轉儲:echo var_export($ qs,true)? – Cesar 2010-04-24 08:34:35
請提供您的需求清楚 – Karthik 2010-04-24 08:39:40