Amit Dhamu

Writes Code

MySQL Results To Array

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
)


mysql / php / arrays

← Serialize and Unserialize Array Variables in LESS →