mysqli_fetch_array while 循环列 [英] mysqli_fetch_array while loop columns

查看:34
本文介绍了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 循环列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆