mysqli_fetch_array while loop columns

ⅰ亾dé卋堺 提交于 2019-11-26 03:59:52

问题


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!


回答1:


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




回答2:


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++;
}



回答3:


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



回答4:


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.




回答5:


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);



回答6:


Try this...

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


来源:https://stackoverflow.com/questions/14456529/mysqli-fetch-array-while-loop-columns

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!