Return to Snippet

Revision: 40118
at January 26, 2011 15:27 by Roaa


Initial Code
<?php

/*
**  AUTHOR  - Ronnie Aarebrot.
**  Script  - Simple MySQL fetch array helper.
**  WebSite - www.RonnieAarebrot.com
**  
**  
**  Description
**  
**  This will return your mysql_fetch_array's into
**  some readable / simple arrays to use. The array
**  key will be the same name as used in your database columns.
**  
**  * HOW TO USE ? *
**
**  $query = mysql_query("SELECT * FROM users");
**  $array = sqlFetch($query);
**  echo "<pre>"; 
**  print_r($array);
**  echo "</pre>";
**
*/

function sqlFetch($query) {
    
    $numFields = mysql_num_fields($query);
    $y = 0;
    while($row = mysql_fetch_array($query)) {
        $x = 0;
        while($x < $numFields) {
            $fieldName = mysql_field_name($query, $x);
            $array[$y][$fieldName] = $row["$fieldName"];
            $x++;
        }
        $y++;
        $x = 0;
    }
    
    return $array;
    
}

/*

* OUTPUT EXAMPLE *

Array
(
    [0] => Array
        (
            [id] => 1
            [username] => roaa
            [password] => 446bcd0025e013351288b5f7999434b42d40916c
            [mail] => [email protected]
            [sex] => male
        )

    [1] => Array
        (
            [id] => 2
            [username] => fungirl
            [password] => 446bcd0025e013351288b5f7999434b42d40916c
            [mail] => [email protected]
            [sex] => female
        )

    [2] => Array
        (
            [id] => 3
            [username] => dem0ns
            [password] => 446bcd0025e013351288b5f7999434b42d40916c
            [mail] => [email protected]
            [sex] => male
        )

    [3] => Array
        (
            [id] => 4
            [username] => hell0
            [password] => 446bcd0025e013351288b5f7999434b42d40916c
            [mail] => [email protected]
            [sex] => male
        )

    [4] => Array
        (
            [id] => 5
            [username] => meddoo
            [password] => 446bcd0025e013351288b5f7999434b42d40916c
            [mail] => [email protected]
            [sex] => female
        )

)

*/

?>

Initial URL

                                

Initial Description

                                

Initial Title
Simple Pretty MySQL Fetch arrays

Initial Tags
array

Initial Language
PHP