-1

Hey usually I use mysqli_fetch_array to display the content of my database in my CMS, but recently someone told me I should use mysqli_fetch_assoc and push the results into an array so that database only runs once instead of running the database for each record.

But I'm not really sure how to display my fields without showing them all, usually I would echo $data['field_name'], but what I've noticed with mysqli_fetch_assoc is I can't just echo $value['field_name'], all I can do is echo $value and it displays all the results.

This is what I've done, hope that makes sense. Thanks in advance for any help!

PHP

$sql = "SELECT * FROM app_categories";

if($result = query($sql)){
    $list = array();

    while($data = mysqli_fetch_assoc($result)){
        array_push($list, $data);
    }

    foreach($list as $i=>$row){      
        foreach($row as $column=>$value){
            echo $value;
        }
     }
}
Passerby
  • 9,715
  • 2
  • 33
  • 50
Mitchell Layzell
  • 3,058
  • 3
  • 20
  • 33
  • Refer to this thread: http://stackoverflow.com/questions/11480129/mysql-fetch-row-vs-mysql-fetch-assoc-vs-mysql-fetch-array – Roman Newaza Mar 21 '13 at 03:10
  • You have what you need in `$list`. Do `foreach ($list as $row) { echo $row['field_name']; }`. It is the unnecessary inner `foreach` you've got there which is tripping you up. – Michael Berkowski Mar 21 '13 at 03:12
  • @MichaelBerkowski Thanks, yeah thats what was really confusing me, that makes a lot more sense, thanks for you help! – Mitchell Layzell Mar 21 '13 at 03:15

1 Answers1

0

why you don't just

echo $row['field_name'];
Mat
  • 6,236
  • 9
  • 42
  • 55