Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.92 KB | None | 0 0
  1. <?php
  2. /*
  3.     Author: Neil Masters[nim at cs.stir.ac.uk]
  4.     This function attempts to be a generic db() application
  5.     that handles any sql query's and returns results in
  6.     an array.
  7.    
  8.     Example usage:
  9.     foreach(dbArray("SELECT * FROM accounts","login,password") as $results)
  10.     {
  11.         $items = explode(",",$results);
  12.         echo '<strong>username:</strong>' .
  13.             $items[0] .
  14.             ', <strong>password:</strong> ' .
  15.             $items[1] . '<br />';
  16.     }
  17. */
  18. function dbArray($query, $returnValues)
  19. {
  20.     $returnValues = explode(",", $returnValues);
  21.     $connect = mysql_connect("server_name","user_name","password") or die("Could not connect to database");
  22.     $dbSelect = mysql_select_db("db_name", $connect) or die("Could not select table");
  23.     $result = mysql_query($query);
  24.     $i = 0;
  25.     while($field = mysql_fetch_assoc($result))
  26.     {
  27.         $returnArray[$i] = $field[$returnValues[0]] . ',' . $field[$returnValues[1]];
  28.         $i++;
  29.     }
  30.     return $returnArray;
  31. }
  32. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement