Mysqli_fetch_array while loop columns

It should be pretty simple, but I can't get it to work. I have this code to repeat the mysqli query:

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): "World of Japan! Welcome to WordPress. This is your first post. Edit or delete it, and then start blogging! " (99 characters) 2 (String): "2Sample PageThis is an example page. It differs from a blog entry because it will remain in one place and appear in the site navigation (in most topics)." (161 characters)

The problem is that it puts all three columns in one column, so I cannot access them separately.

This is for example:

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

Must be divided by 4, check, test

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 prints only 1 line instead of all three ...

Any help is much appreciated!

+4
source share
6 answers

Get all values ​​from 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 repeat the values. Or get each item from the $ post variable

+18
source

I think this will be an easier way to output your results.

Sorry to use my own data, it's 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 will lead to the conclusion:

  • - Identifier Name
  • -1 gary
  • -2 John
  • -3 michaels
+3
source

It's your decision.

 $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++; } 
+2
source

Both will work fine in mysqli_fetch_array during loops

 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 a second argument to $ resulttype.

MYSQLI_ASSOC: Get associative array

MYSQLI_NUM: Get a numeric array

MYSQLI_BOTH: Select both an associative and a numerical array.

-1
source

Try the following:

  $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); 
-2
source

Try it...

 while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) { 
-3
source

All Articles