The missing php function: mysql_fetch_all

The function:

1
2
3
4
5
6
7
8
function mysql_fetch_all($query, $kind = 'assoc')
{
    $result = array();
    $kind = $kind === 'assoc' ? $kind : 'row';
    eval('while(@$r = mysql_fetch_' . $kind . '($query)) array_push($result, $r);');
    return $result;

}

Usage:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$query = "SELECT * FROM my_table;"
$res = mysql_query($query, $mysqll);
$res_array = mysql_fetch_all($res);

print_r($res_array);

/*
Output:
Array
(
    [0] => Array
        (
            [col_A] => val_A1
            [col_b] => val_B1
        )
    [1] => Array
        (
            [col_A] => val_A2
            [col_b] => val_B2
        )
* /

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>