php mysqli_fetch_array while 循环列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14456529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:17:21  来源:igfitidea点击:

mysqli_fetch_array while loop columns

phpmysqliwhile-loopmultiple-columns

提问by Sebastian

Should be pretty basic, but I can't get it to work. I have this code to iterate over a mysqli query:

应该是非常基本的,但我无法让它工作。我有这个代码来迭代一个 mysqli 查询:

while($row = mysqli_fetch_array($result)) {
    $posts[] = $row['post_id'].$row['post_title'].$row['content'];
}

It works and returns:

它工作并返回:

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)

变量#1:(数组,3 个元素)?0(字符串):“4testtest”(9 个字符) 1(字符串):“1Hello world!欢迎来到 WordPress。这是您的第一篇文章。编辑或删除它,然后开始写博客!” (99 个字符)2(字符串):“2Sample PageThis 是一个示例页面。它与博客文章不同,因为它会停留在一个地方,并会显示在您的网站导航中(在大多数主题中)。” (161 个字符)

The problem is that it puts all three colums into one column, so I can't access them seperatly.

问题是它将所有三个列都放在一列中,所以我无法单独访问它们。

This for example:

这例如:

0 (String): "4testtest" (9 characters)

0(字符串):“4testtest”(9 个字符)

Should be seperated into 4, test, test

应该分为4个,测试,测试

When I do this:

当我这样做时:

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'];  
}   

It only outputs 1 row instead of all three …

它只输出 1 行而不是所有三行……

Any help is greatly appreciated!

任何帮助是极大的赞赏!

回答by Ricardo Ortega Maga?a

Get all the values from MySQL:

从 MySQL 获取所有值:

    $post = array();
    while($row = mysql_fetch_assoc($result))
    {
        $posts[] = $row;
    }

Then, to get each value:

然后,获取每个值:

<?php 
     foreach ($posts as $row) 
        { 
            foreach ($row as $element)
            {
                echo $element."<br>";
            }
        }
?>

To echo the values. Or get each element from the $post variable

呼应这些值。或者从 $post 变量中获取每个元素

回答by Bogdan Lozyak

This one was your solution.

这是您的解决方案。

$x = 0;
while($row = mysqli_fetch_array($result)) {             
    $posts[$x]['post_id'] = $row['post_id'];
    $posts[$x]['post_title'] = $row['post_title'];
    $posts[$x]['type'] = $row['type'];
    $posts[$x]['author'] = $row['author'];
    $x++;
}

回答by 05beckga

I think this would be a more simpler way of outputting your results.

我认为这将是一种更简单的输出结果的方式。

Sorry for using my own data should be easy to replace .

抱歉使用我自己的数据应该很容易替换。

$query = "SELECT * FROM category ";

$result = mysqli_query($connection, $query);


    while($row = mysqli_fetch_assoc($result))
    {
        $cat_id = $row['cat_id'];
        $cat_title = $row['cat_title'];

        echo $cat_id . " " . $cat_title  ."<br>";
    }

This would output :

这将输出:

  • -ID Title
  • -1 Gary
  • -2 John
  • -3 Michaels
  • -身标题
  • -1 加里
  • -2 约翰
  • -3 迈克尔斯

回答by Elangovan

Both will works perfectly in mysqli_fetch_arrayin while loops

两者都可以在 while 循环中的mysqli_fetch_array中完美运行

while($row = mysqli_fetch_array($result,MYSQLI_BOTH)) {
    $posts[] = $row['post_id'].$row['post_title'].$row['content'];
}

(OR)

(或者)

while($row = mysqli_fetch_array($result,MYSQLI_ASSOC)) {
    $posts[] = $row['post_id'].$row['post_title'].$row['content'];
}

mysqli_fetch_array()- has second argument $resulttype.

mysqli_fetch_array()- 有第二个参数 $resulttype。

MYSQLI_ASSOC:Fetch associative array

MYSQLI_ASSOC:获取关联数组

MYSQLI_NUM:Fetch numeric array

MYSQLI_NUM:获取数值数组

MYSQLI_BOTH:Fetch both associative and numeric array.

MYSQLI_BOTH:获取关联数组和数值数组。

回答by Fero

Try this :

尝试这个 :

   $i = 0;    
    while($row = mysqli_fetch_array($result)) {  

            $posts['post_id'] = $row[$i]['post_id'];
            $posts['post_title'] = $row[$i]['post_title'];
            $posts['type'] = $row[$i]['type'];
            $posts['author'] = $row[$i]['author'];  

        }   
    $i++;
    }

print_r($posts);

回答by Pepito Rosales

Try this...

尝试这个...

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {