8 May 2011

How to turn MySQL query into an array in PHP. Print query results on screen. This is useful for determining contents of a table.

Source code viewer
  1. // The database connection.
  2. $link = mysql_connect('localhost', 'username', 'password');
  3. mysql_select_db('database', $link);
  4.  
  5. // SQL query.
  6. $result = mysql_query('SELECT * FROM Table');
  7.  
  8. // Fetch results and write them into an array.
  9. $array = array();
  10. while ($row = mysql_fetch_array($result)) {
  11. $array[] = $row;
  12. }
  13.  
  14. // Pretty print array.
  15. print_r($array);
  16.  
  17. // Close mysql connection.
  18. mysql_close($link);
Programming Language: PHP