mysqli_fetch_array while循环列 [英] mysqli_fetch_array while loop columns
问题描述
应该很基本,但是我无法使它正常工作.我有以下代码可以遍历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 :(数组,3个元素)↵ 0(字符串):"4testtest"(9个字符) 1(字符串):"1Hello world!欢迎使用WordPress.这是您的第一篇文章.编辑或删除它,然后开始写博客!" (99个字符) 2(字符串):"2Sample Page这是一个示例页面.它与博客文章不同,因为它将停留在一个位置并显示在 您的网站导航(在大多数主题中)."(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)
应该分为4个,测试,测试
Should be seperated into 4, test, test
当我这样做时:
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
这篇关于mysqli_fetch_array while循环列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!