1

I am retrieving the rows of my query as such:

$rows = mysqli_fetch_all($result, MYSQLI_ASSOC);

How can I do:

(PSEUDO CODE)
for each row in rows
  echo row

I can just "echo $rows" but that doesn't allow me to go through each row on a separate paragraph.

As a second question, how can I go through each column of a row:

(PSEUDO CODE)
for each row in rows
  for each column in row
    echo column
Juicy
  • 11,840
  • 35
  • 123
  • 212
  • 1
    Your pseudo code is pretty much spot on, `mysqli_fetch_all` returns a array with all rows, each row is represented by a array with key => value pairs. What part of the code do you have a problem with? – hank Aug 12 '13 at 12:22
  • searching this question here will give you many answers such as http://stackoverflow.com/questions/1501274/get-array-of-rows-with-mysqli-result . – Taha Paksu Aug 12 '13 at 12:22
  • If you are still around, please accept the answer from @ale – Your Common Sense May 27 '16 at 12:39

4 Answers4

4

You pseudo code is all fine.. you just need to turn it into PHP....

Answer to first question:

// Loop through each row and print it
foreach( $rows as $row ):
   print_r( $row )
endforeach;

Second question.. something like:

foreach( $rows as $row ):
   foreach( $row as $col ):
      echo $col;
   endforeach;    
endforeach;
ale
  • 11,636
  • 27
  • 92
  • 149
4

I would use something like this:

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
    echo $row['name_of_your_column'];
}

where result is:

$result = mysqli_query($connection, $query);
Marko Ćilimković
  • 734
  • 1
  • 8
  • 22
1

I have written a small code, hope it solves the query.

    //Query to be executed;
$qry="select * from `tableName`";
//Performs a query on the database;
//here conn is a link identifier returned by mysqli_connect or mysqli_init function;
$result=mysqli_query($conn,$qry);
//Gets the number of rows in a result;
$rowcount=mysqli_num_rows($result);
//Fetches all result rows as an associative array;
$row = mysqli_fetch_all($result,MYSQLI_ASSOC);
//Iterating each row;
for($i=0;$i<$rowcount;$i++)
{
    echo "<br> ".$row[$i]['`column_name_1`']." ".$row[$i]['`column_name_2`']." ".$row[$i]['`column_name_3`'];
}

Example Database Table Mysql Table snapshot Code

//here conn is a link identifier returned by mysqli_connect or mysqli_init function;
$conn = mysqli_connect("localhost","root","","nsgdb") or die('Error connecting to MySQL server.');
//Query to be executed;
$qry="select * from users";
//Performs a query on the database;
$result=mysqli_query($conn,$qry);
//Gets the number of rows in a result;
$rowcount=mysqli_num_rows($result);
//Fetches all result rows as an associative array;
$row = mysqli_fetch_all($result,MYSQLI_ASSOC);
//Iterating each row;
for($i=0;$i<$rowcount;$i++)
{
   echo "<br> ".$row[$i]['id']." ".$row[$i]['user']." ".$row[$i]['pass'];
}

Output of the code

  • Nope, it doesn't – Your Common Sense Mar 17 '17 at 06:23
  • I have added the snapshots and running code example for you. – Sahib Singh Mar 17 '17 at 06:58
  • 1
    PHP mySQLi_fetch_all: iterate through each row I have just provided another way of doing it. I respect you knowledge boss :) I think we can provide different ways of doing the same thing on stackoverflow..!! If you still think my answer is not relevant to the question then can you please spot the part where i'm lacking. – Sahib Singh Mar 17 '17 at 09:47
0
$rows = mysqli_fetch_assoc($result, MYSQLI_ASSOC);

Use the mysqli_fetch_assoc function.

And the use the foreach as:

foreach($rows as $column => $value) {
  echo $column." = ".$value;
}

Note: You can also use foreach with mysqli_fetch_all.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75