10

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

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)

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)

Should be seperated into 4, test, 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 only outputs 1 row instead of all three …

Any help is greatly appreciated!

Jaak Kütt
  • 2,566
  • 4
  • 31
  • 39
Sebastian
  • 923
  • 1
  • 9
  • 23

6 Answers6

28

Get all the values from MySQL:

    $post = array();
    while($row = mysqli_fetch_array($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

7

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++;
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
Bogdan Lozyak
  • 87
  • 1
  • 1
5

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
05beckga
  • 95
  • 2
  • 9
1

Both will works perfectly in mysqli_fetch_array in while 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 second argument $resulttype.

MYSQLI_ASSOC: Fetch associative array

MYSQLI_NUM: Fetch numeric array

MYSQLI_BOTH: Fetch both associative and numeric array.

Elangovan
  • 3,469
  • 4
  • 31
  • 38
  • 1
    It is real time example and it is working perfectly for above mentioned code, then why you are given negative rating. If you used above code and anything fails means just reported me, this is reason and gave negative. – Elangovan Feb 14 '17 at 07:03
-2

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);
Fero
  • 12,969
  • 46
  • 116
  • 157
-3

Try this...

while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
Jose Ricardo Bustos M.
  • 8,016
  • 6
  • 40
  • 62
  • This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient [reputation](http://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](http://stackoverflow.com/help/privileges/comment). – Parixit Oct 29 '15 at 09:07
  • 2
    @Parixit Looks like an answer to me? Not sure if it is correct or not, but that is not important for the review. – Anders Oct 29 '15 at 13:20