Php – how to echo back the whole thesql_fetch_array

PHP

i have this SQL code :

$sql="SELECT * FROM functions
      WHERE user_name='ben ' 
      AND (function_description LIKE '%xxx%') ";

it give me back two lines on phpmyadmin SQL :

user_name   function_type   function_name   function_description
ben         PHP         PHP1            that xxx
ben         PHP         PHP2            lofaif adnan xxx i

but when i tried to echo back those lines using this :

$results=mysql_query($sql,$connection);
 while($row=mysql_fetch_array($results, MYSQL_ASSOC))
{
    echo $row['function_name'] . " : " . $row['function_description'] . "<br/><br/>";
}

it give me back just this :

PHP2:lofaif adnan xxx i

and i want to echo back the 2 lines .. whats the problem with it ?

Best Answer

The while loop you are currently in will only print out the current iteration. To print out all rows in the DB simply initialise the $rows variable as an array $rows = array(); and assign all the rows to it via while($rows[]=mysql_fetch_array($results, MYSQL_ASSOC)). Your $rows variable should now contain all the DB records.

$results=mysql_query($sql,$connection);
$row = array();
while($rows[]=mysql_fetch_array($results));

//should not contain all rows
print_r( $rows );