home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / sqlhelp.php3.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  7.8 KB  |  289 lines

  1. SQL helper functions 
  2.  
  3. A set of functions sitting on top of the abstraction layer that makes it a little easier to do SQL stuff. Documentation is within. 
  4.  
  5.  
  6. <?php  /* -*- C++ -*- */ 
  7. /* 
  8.  * $Id: sql.phl,v 1.5 1998/07/02 09:52:13 ssb Exp $ 
  9.  */ 
  10.  
  11. /* 
  12.  * ABOUT: 
  13.  * 
  14.  * Lazy PHP programmer series: SQL helper functions. 
  15.  * Written by Stig Bakken <ssb@guardian.no> 
  16.  * 
  17.  * CONFIGURATION: 
  18.  * 
  19.  * You must set some global variables prior to including this library: 
  20.  * $db_type          which database abstraction layer to use. 
  21.  *                   If your layer is defined in "db-odbc.phl", 
  22.  *                   set this variable to "odbc". 
  23.  * $db_dsn           Which database to connect to (dsn=data source name) 
  24.  * $db_user          Which user to connect as 
  25.  * $db_pass          Password used when connecting 
  26.  * 
  27.  * Some optional global variables can be set: 
  28.  * $sql_debug        true/false, toggles debug information 
  29.  * $sql_log_facility how to log debug information 
  30.  * $sql_log_dest     debug information destination 
  31.  * 
  32.  * $sql_log_facility and $sql_log_dest are passed as the second 
  33.  * and third args to PHP's error_log() function, respectively. 
  34.  * The default is debugging through the PHP TCP debugger to 
  35.  * port 1400 on localhost.  Debug information is also disabled 
  36.  * by default. 
  37.  * 
  38.  * The following global variables will be defined: 
  39.  * $db_connection  The opened connection. 
  40.  * 
  41.  * 
  42.  * USAGE: 
  43.  * 
  44.  * All you have to do is require("sql.phl"), either explicitly in the 
  45.  * pages where you want to use it, or through auto_prepend_file somehow. 
  46.  * Being a lazy programmer, I always include everything I need with 
  47.  * auto-prepend from a file I call "config.phl" looking something like 
  48.  * this: 
  49.  * 
  50.  * <?php 
  51.  * $db_type = "odbc"; 
  52.  * $db_user = "myuser"; 
  53.  * $db_pass = "mypassword"; 
  54.  * $db_dsn = "SVT=Solid; DSN=MyDB" 
  55.  * require("sql.phl"); 
  56.  * ?> 
  57.  * 
  58.  * Once you have included this file, a database connection will 
  59.  * be opened for you once it is needed, and this connection will 
  60.  * be used for all queries.  Memory is freed automatically for 
  61.  * all functions except sql(). 
  62.  * 
  63.  * Example use of all functions:  
  64.  * 
  65.  * // returns a result identifier 
  66.  * $res = sql("SELECT * FROM table"); 
  67.  * 
  68.  * // returns one column: 
  69.  * $name = sql_one("SELECT name FROM table WHERE id = $id"); 
  70.  * 
  71.  * // returns one row in an array 
  72.  * $row = sql_onerow("SELECT * FROM table WHERE id = $id"); 
  73.  * 
  74.  * // returns an array of arrays with all the result data 
  75.  * $data = sql_all("SELECT * FROM table"); 
  76.  * 
  77.  * // returns an array with data from column 0 only: 
  78.  * $names = sql_col("SELECT name FROM table", 0); 
  79.  * 
  80.  * // returns an associative array with the first column as the 
  81.  * // key and the second column as the value: 
  82.  * $name_by_id = sql_assoc("SELECT id, name FROM table"); 
  83.  * 
  84.  * // enable auto-commit 
  85.  * sql_autocommit(true); 
  86.  * 
  87.  * // disable auto-commit 
  88.  * sql_autocommit(false); 
  89.  * 
  90.  * // commit transaction 
  91.  * sql_commit(); 
  92.  * 
  93.  * // roll back transaction 
  94.  * sql_rollback(); 
  95.  * 
  96.  */ 
  97.  
  98. require( "db-$db_type.phl"); 
  99.  
  100. $db_connection = 0; 
  101.  
  102. /* debugging defaults */ 
  103. if (empty($sql_debug)) { 
  104.     $sql_debug = false; 
  105. if (empty($sql_log_facility)) { 
  106.     $sql_log_facility = 2;  /* debug through TCP */ 
  107. if (empty($sql_log_dest)) { 
  108.     $sql_log_dest =  "127.0.0.1:1400";  /* destination */ 
  109.  
  110. /* 
  111.  * Function: sql_debug 
  112.  * Description: sends debug information somewhere if 
  113.  * the global variable $sql_debug is true. 
  114.  */ 
  115. function sql_debug($message) { 
  116.     global $sql_debug, $sql_log_facility, $sql_log_dest; 
  117.     if ($sql_debug) { 
  118.     error_log( "[SQL] $message", $sql_log_facility, $sql_log_dest); 
  119.     } 
  120.  
  121. /* 
  122.  * Function: assert_db_connection 
  123.  * Description: makes sure we have a database connection 
  124.  */ 
  125. function assert_db_connection() 
  126.     global $db_connection; 
  127.     if ($db_connection) { 
  128.     return; 
  129.     } 
  130.     global $db_dsn, $db_user, $db_pass; 
  131.     $db_connection = db_connect($db_dsn, $db_user, $db_pass); 
  132.     if (!$db_connection) { 
  133.     die( "Failed to connect to database."); 
  134.     } 
  135.  
  136. /* 
  137.  * Function: sql 
  138.  * Arguments: $query (string)   - SQL statement 
  139.  * Description: executes an SQL statement 
  140.  * Returns: (int) result identifier 
  141.  *          returns 0 upon error 
  142.  */ 
  143. function sql($query) { 
  144.     global $db_connection, $PHP_SELF; 
  145.     assert_db_connection(); 
  146.     sql_debug($query); 
  147.     return db_query($db_connection, $query); 
  148.  
  149. /* 
  150.  * Function: sql_onerow 
  151.  * Arguments: $query (string)   - SQL statement 
  152.  * Description: executes an SQL statement and returns the first 
  153.  *              row of the result 
  154.  * Returns: (array) the first row 
  155.  *          returns 0 upon error 
  156.  */ 
  157. function sql_onerow($query) { 
  158.     $res = sql($query); 
  159.     $row = db_fetch_row($res); 
  160.     db_free_result($res); 
  161.     return $row; 
  162.  
  163. /* 
  164.  * Function: sql_one 
  165.  * Arguments: $query (string)   - SQL statement 
  166.  * Description: executes an SQL statement and returns the first 
  167.  *              column of the first row of the result 
  168.  * Returns: (mixed) the first column of the first row 
  169.  *          returns false upon error 
  170.  */ 
  171. function sql_one($query) { 
  172.     $row = sql_onerow($query); 
  173.     if (gettype($row) ==  "array") { 
  174.     return $row[0]; 
  175.     } else { 
  176.     return false; 
  177.     } 
  178.  
  179. /* 
  180.  * Function: sql_all 
  181.  * Arguments: $query (string)   - SQL statement 
  182.  * Description: executes an SQL statement and returns an array 
  183.  *              of arrays with the rows and columns of all the 
  184.  *              result data from the query. 
  185.  * Returns: array of arrays or false on error 
  186.  */ 
  187. function sql_all($query) { 
  188.     $res = sql($query); 
  189.     if ($res) { 
  190.     $all = array(); 
  191.     while ($row = db_fetch_row($res)) { 
  192.         $all[] = $row; 
  193.     } 
  194.     db_free_result($res); 
  195.     return $all; 
  196.     } 
  197.     return false; 
  198.  
  199.  
  200. /* 
  201.  * Function: sql_col 
  202.  * Arguments: $query (string)   - SQL statement 
  203.  *            $column (int)     - returned result column number 
  204.  * Description: executes an SQL statement and returns an array 
  205.  *              with the results from a specific column in the result. 
  206.  * Returns: array or false on error 
  207.  */ 
  208. function sql_col($query, $column) { 
  209.     $res = sql($query); 
  210.     if ($res) { 
  211.     $all = array(); 
  212.     while ($row = db_fetch_row($res)) { 
  213.         $all[] = $row[$column]; 
  214.     } 
  215.     db_free_result($res); 
  216.     return $all; 
  217.     } 
  218.     return false; 
  219.  
  220.  
  221. /* 
  222.  * Function: sql_assoc 
  223.  * Arguments: $query (string)   - SQL statement 
  224.  * Description: executes an SQL statement and returns an associative 
  225.  *              array.  The indices of this array are taken from the 
  226.  *              first column of the result, while the values are taken 
  227.  *              from the second column.  If there are more than two 
  228.  *              columns the remaining ones are ignored. 
  229.  * Returns: associative array or false on error 
  230.  */ 
  231. function sql_assoc($query) { 
  232.     $res = sql($query); 
  233.     if ($res) { 
  234.     $row = db_fetch_row($res); 
  235.     if (!is_array($row) || count($row) < 2) { 
  236.         db_free_result($res); 
  237.         return false; 
  238.     } 
  239.     while ($row) { 
  240.         $assoc[$row[0]] = $row[1]; 
  241.         $row = db_fetch_row($res); 
  242.     } 
  243.     db_free_result($res); 
  244.     return $assoc; 
  245.     } 
  246.     return false; 
  247.  
  248.  
  249. function sql_autocommit($enabled) 
  250.     global $db_connection; 
  251.     if (!$enabled) { 
  252.     sql_debug( "transaction starting"); 
  253.     } 
  254.     return db_autocommit($db_connection, $enabled); 
  255.  
  256.  
  257. function sql_commit() 
  258.     global $db_connection; 
  259.     sql_debug( "transaction complete"); 
  260.     return db_commit($db_connection); 
  261.  
  262.  
  263. function sql_rollback() 
  264.     global $db_connection; 
  265.     sql_debug( "transaction aborted"); 
  266.     return db_rollback($db_connection); 
  267.  
  268.  
  269. ?> 
  270.  
  271.