tags:

views:

64

answers:

3

How can use foreach loop to loop through the $Result?

<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or die("Could not connect: " . mysql_error());
mysql_select_db("mydb");

$Query  = mysql_query("SELECT * FROM mytable");
$Result = array( );

while ($Row = mysql_fetch_array ( $Query) ) {
    $Result [ ] = $Row;  
}

mysql_free_result($Query);

print_r ($Result);

?>

I just have very vague idea:

<?php

foreach ($Result )
{
 echo $row[fname] . ' ' . $row[lname] . ' ' $row[email];
}

?>

Could someone help please?

A: 

Something like the following

foreach($Result as $row){
   $fname=$row['fname'];
   // etc...
}
Yehonatan
A: 

why do you want to do the indirect way with that array? you could instead simpply do this:

$Query  = mysql_query("SELECT * FROM mytable");

while ($Row = mysql_fetch_array ( $Query) ) {
  echo $Row[fname] . ' ' . $Row[lname] . ' ' $Row[email]; 
}

mysql_free_result($Query);
oezi
A: 
$link = mysqli_link('host', 'user', 'pass', 'db');
$query = 'SELECT * FROM mytable';
$result = mysqli_query($link, $query)
while ($row =  mysqli_fetch_assoc($result)) {
  $out .= $row['fname'] . ' ' . $row['lname'] . ' ' . $row['email'];
}
echo $out;
Majid