MySQL Results To Array

Submitted by:Jhon Jhon

Date added:14 September, 2013

Category:MySQL

An example for MySQL Results To Array

Tags: array

Code Snippet:

<Function>

function SQLQuery($query) {
$results = array();
$query = mysql_query($query);
$num_rows = mysql_num_fields($query);
$y = 0;

while($row = mysql_fetch_array($query)) {
$x = 0;
while($x < $num_rows) {
$field = mysql_field_name($query, $x);
$results[$y][$field] = $row[$field];
$x++;
}
$y++;
$x = 0;
}
return $results;
}



<Usage>

$users = SQLQuery("SELECT * FROM users");

// Output of $users
Array
(
[0] => Array
(
[user_id] => 1
[user_f_name] => Amit
[user_m_name] =>
[user_l_name] => Dhamu
)
......etc
)
 
 

Comments