Posted By

Roaa on 01/26/11


Tagged

array


Versions (?)

Who likes this?

10 people have marked this snippet as a favorite

Ult1Mate
Roaa
BuRn
Solblem
fomigo
Some0ne
HelloKit
jbyerson
ringo380
Priestd09


Simple Pretty MySQL Fetch arrays


 / Published in: PHP
 

  1. <?php
  2.  
  3. /*
  4. ** AUTHOR - Ronnie Aarebrot.
  5. ** Script - Simple MySQL fetch array helper.
  6. ** WebSite - www.RonnieAarebrot.com
  7. **
  8. **
  9. ** Description
  10. **
  11. ** This will return your mysql_fetch_array's into
  12. ** some readable / simple arrays to use. The array
  13. ** key will be the same name as used in your database columns.
  14. **
  15. ** * HOW TO USE ? *
  16. **
  17. ** $query = mysql_query("SELECT * FROM users");
  18. ** $array = sqlFetch($query);
  19. ** echo "<pre>";
  20. ** print_r($array);
  21. ** echo "</pre>";
  22. **
  23. */
  24.  
  25. function sqlFetch($query) {
  26.  
  27. $numFields = mysql_num_fields($query);
  28. $y = 0;
  29. while($row = mysql_fetch_array($query)) {
  30. $x = 0;
  31. while($x < $numFields) {
  32. $fieldName = mysql_field_name($query, $x);
  33. $array[$y][$fieldName] = $row["$fieldName"];
  34. $x++;
  35. }
  36. $y++;
  37. $x = 0;
  38. }
  39.  
  40. return $array;
  41.  
  42. }
  43.  
  44. /*
  45.  
  46. * OUTPUT EXAMPLE *
  47.  
  48. Array
  49. (
  50.   [0] => Array
  51.   (
  52.   [id] => 1
  53.   [username] => roaa
  54.   [password] => 446bcd0025e013351288b5f7999434b42d40916c
  55.   [mail] => [email protected]
  56.   [sex] => male
  57.   )
  58.  
  59.   [1] => Array
  60.   (
  61.   [id] => 2
  62.   [username] => fungirl
  63.   [password] => 446bcd0025e013351288b5f7999434b42d40916c
  64.   [mail] => [email protected]
  65.   [sex] => female
  66.   )
  67.  
  68.   [2] => Array
  69.   (
  70.   [id] => 3
  71.   [username] => dem0ns
  72.   [password] => 446bcd0025e013351288b5f7999434b42d40916c
  73.   [mail] => [email protected]
  74.   [sex] => male
  75.   )
  76.  
  77.   [3] => Array
  78.   (
  79.   [id] => 4
  80.   [username] => hell0
  81.   [password] => 446bcd0025e013351288b5f7999434b42d40916c
  82.   [mail] => [email protected]
  83.   [sex] => male
  84.   )
  85.  
  86.   [4] => Array
  87.   (
  88.   [id] => 5
  89.   [username] => meddoo
  90.   [password] => 446bcd0025e013351288b5f7999434b42d40916c
  91.   [mail] => [email protected]
  92.   [sex] => female
  93.   )
  94.  
  95. )
  96.  
  97. */
  98.  
  99. ?>

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Ult1Mate on January 26, 2011

Nice! love this function, makes it so easy!

Posted By: BuRn on January 27, 2011

Thanks! nice function, works great and so easy to use

Posted By: Solblem on January 27, 2011

thanks for sharing!

Posted By: nilambar on February 9, 2011

Nice. Very useful function. :-)

You need to login to post a comment.