問題描述
應(yīng)該是非常基本的,但我無法讓它工作.我有這個代碼來迭代一個 mysqli 查詢:
Should be pretty basic, but I can't get it to work. I have this code to iterate over a mysqli query:
while($row = mysqli_fetch_array($result)) {
$posts[] = $row['post_id'].$row['post_title'].$row['content'];
}
它有效并返回:
變量#1:(數(shù)組,3 個元素)?0(字符串):4testtest"(9 個字符)1(字符串):1Hello world!歡迎來到 WordPress.這是您的第一篇文章.編輯或刪除它,然后開始寫博客!"(99 個字符)2(字符串):2Sample PageThis is an example page.它與博客文章不同,因為它會留在一個地方,并會出現(xiàn)在您的網(wǎng)站導(dǎo)航(在大多數(shù)主題中)."(161 個字符)
Variable #1: (Array, 3 elements) ? 0 (String): "4testtest" (9 characters) 1 (String): "1Hello world!Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!" (99 characters) 2 (String): "2Sample PageThis is an example page. It's different from a blog post because it will stay in one place and will show up in your site navigation (in most themes)." (161 characters)
問題是它將所有三個列放在一列中,所以我無法單獨訪問它們.
The problem is that it puts all three colums into one column, so I can't access them seperatly.
例如:
0(字符串):4testtest"(9 個字符)
0 (String): "4testtest" (9 characters)
應(yīng)該分為4個,測試,測試
Should be seperated into 4, test, test
當(dāng)我這樣做時:
while($row = mysqli_fetch_array($result)) {
$posts['post_id'] = $row['post_id'];
$posts['post_title'] = $row['post_title'];
$posts['type'] = $row['type'];
$posts['author'] = $row['author'];
}
它只輸出 1 行而不是所有三行......
It only outputs 1 row instead of all three …
非常感謝任何幫助!
推薦答案
從 MySQL 獲取所有值:
Get all the values from MySQL:
$post = array();
while($row = mysql_fetch_assoc($result))
{
$posts[] = $row;
}
然后,獲取每個值:
<?php
foreach ($posts as $row)
{
foreach ($row as $element)
{
echo $element."<br>";
}
}
?>
回顯值.或者從 $post 變量中獲取每個元素
To echo the values. Or get each element from the $post variable
這篇關(guān)于mysqli_fetch_array while 循環(huán)列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!