mysqli_fetch_array while loop columns(mysqli_fetch_array while 循环列)
问题描述
应该是非常基本的,但我无法让它工作.我有这个代码来迭代一个 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 PageThis is an example 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 循环列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:mysqli_fetch_array while 循环列


- Laravel 仓库 2022-01-01
- SoapClient 设置自定义 HTTP Header 2021-01-01
- 没有作曲家的 PSR4 自动加载 2022-01-01
- PHP Count 布尔数组中真值的数量 2021-01-01
- Mod使用GET变量将子域重写为PHP 2021-01-01
- 正确分离 PHP 中的逻辑/样式 2021-01-01
- 如何定位 php.ini 文件 (xampp) 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01
- 从 PHP 中的输入表单获取日期 2022-01-01
- 带有通配符的 Laravel 验证器 2021-01-01