Your browser must have JavaScript enabled in order to view this page.
 >  >
 
Welcome Guest#216 Login/register    Go to Bottom
Go to Top

Source for file dpdb_mysql.php

Documentation is available at dpdb_mysql.php

  1. <?php
  2. /**
  3.  * MySQL database functions
  4.  *
  5.  * A very simple layer over MySQL functions, providing the same interface as
  6.  * dpdb_mdb2.php (which is a layer over the MDB2 database abstraction layer).
  7.  * Either this file or dpdb_mdb2.php is included, based on settings in
  8.  * dpuniverse-ini.php.
  9.  *
  10.  * DutchPIPE version 0.4; PHP version 5
  11.  *
  12.  * LICENSE: This source file is subject to version 1.0 of the DutchPIPE license.
  13.  * If you did not receive a copy of the DutchPIPE license, you can obtain one at
  14.  * http://dutchpipe.org/license/1_0.txt or by sending a note to
  15.  * license@dutchpipe.org, in which case you will be mailed a copy immediately.
  16.  *
  17.  * @package    DutchPIPE
  18.  * @subpackage lib
  19.  * @author     Lennert Stock <ls@dutchpipe.org>
  20.  * @copyright  2006, 2007 Lennert Stock
  21.  * @license    http://dutchpipe.org/license/1_0.txt  DutchPIPE License
  22.  * @version    Subversion: $Id: dpdb_mysql.php 293 2007-08-25 23:11:20Z ls $
  23.  * @link       http://dutchpipe.org/manual/package/DutchPIPE
  24.  * @see        dpuniverse-ini.php, dpdb_mdb2.php
  25.  * @since      DutchPIPE 0.4.0
  26.  */
  27.  
  28. /**
  29.  * MySQL link identifier
  30.  */
  31. $grMySqlConnection NULL;
  32.  
  33. /**
  34.  * Connects to the database server and sets it the current database
  35.  *
  36.  * Prints an error message to the server output in case of an error
  37.  *
  38.  * @return     boolean   MySQL link identifier on success, FALSE on failure
  39.  * @see        DPUNIVERSE_MYSQL_HOST, PUNIVERSE_MYSQL_USER,
  40.  *              DPUNIVERSE_MYSQL_PASSWORD, http://www.php.net/mysql_connect,
  41.  *              http://www.php.net/mysql_select_db, dp_db_query, dp_db_exec,
  42.  *              dp_db_fetch_one, dp_db_fetch_row, dp_db_quote, dp_db_num_rows,
  43.  *              dp_db_next_id, dp_db_free
  44.  */
  45. function &dp_db_connect()
  46. {
  47.     global $grMySqlConnection;
  48.  
  49.     if ($grMySqlConnection{
  50.         return $grMySqlConnection;
  51.     }
  52.  
  53.         DPUNIVERSE_MYSQL_PASSWORD);
  54.     if (!is_resource($connection)) {
  55.         echo sprintf(dp_text("Could not connect: %s [error number %d]\n"),
  56.             mysql_error()mysql_errno());
  57.         $rval FALSE;
  58.     else {
  59.         $grMySqlConnection =$connection;
  60.  
  61.         if (!mysql_select_db(DPUNIVERSE_MYSQL_DB$connection)) {
  62.             echo sprintf(dp_text("Failed to select database: %s\n"),
  63.                 DPUNIVERSE_MYSQL_DB);
  64.             $rval FALSE;
  65.         else {
  66.             return $connection;
  67.         }
  68.     }
  69.  
  70.     return $rval;
  71. }
  72.  
  73. /**
  74.  * Sends a MySQL query of type SELECT, SHOW, EXPLAIN, DESCRIBE, ...
  75.  *
  76.  * Prints an error message to the server output in case of an error
  77.  *
  78.  * @param      string    $sql        SQL statement
  79.  * @return     mixed     a MySQL resource or FALSE on failure
  80.  * @see        http://www.php.net/mysql_query, dp_db_connect, dp_db_exec,
  81.  *              dp_db_fetch_one, dp_db_fetch_row, dp_db_quote, dp_db_num_rows,
  82.  *              dp_db_free
  83.  */
  84. function dp_db_query($sql)
  85. {
  86.     if (!($link &dp_db_connect())) {
  87.         return FALSE;
  88.     }
  89.  
  90.     $result mysql_query($sql$link);
  91.     if (FALSE === $result{
  92.         echo mysql_error($link' [error number ' mysql_errno($link)
  93.             . "]\n";
  94.     }
  95.  
  96.     return $result;
  97. }
  98.  
  99. /**
  100.  * Sends a MySQL query of type DELETE, INSERT, REPLACE, UPDATE, ...
  101.  *
  102.  * Prints an error message to the server output in case of an error
  103.  *
  104.  * @param      string    $sql        SQL statement
  105.  * @return     mixed     number of affected rows or FALSE on failure
  106.  * @see        http://www.php.net/mysql_query, dp_db_connect, dp_db_query,
  107.  *              dp_db_quote, dp_db_next_id
  108.  */
  109. function dp_db_exec($sql)
  110. {
  111.     if (!($link &dp_db_connect())) {
  112.         return FALSE;
  113.     }
  114.  
  115.     $result mysql_query($sql$link);
  116.     if (FALSE === $result
  117.             || -=== ($affected_rows mysql_affected_rows($link))) {
  118.         echo mysql_error($link' [error number ' mysql_errno($link)
  119.             . "]\n";
  120.         return FALSE;
  121.     }
  122.  
  123.     return $affected_rows;
  124. }
  125.  
  126. /**
  127.  * Gets result data
  128.  *
  129.  * Prints an error message to the server output in case of an error
  130.  *
  131.  * @param      resource  $result     result resource from dp_db_query()
  132.  * @param      int       $row        row number from result, starts at 0
  133.  * @param      int       $field      optional field name or offset, 0 by default
  134.  * @return     mixed     string with contents of one cell, or FALSE on failure
  135.  * @see        http://www.php.net/mysql_result, dp_db_connect, dp_db_query,
  136.  *              dp_db_fetch_row, dp_db_quote, dp_db_num_rows, dp_db_free
  137.  */
  138. function dp_db_fetch_one($result$row$field 0)
  139. {
  140.     $one mysql_result($result$row$field);
  141.     if (FALSE === $one{
  142.         echo "dp_db_fetch_one: Failed to fetch data\n";
  143.     }
  144.  
  145.     return $one;
  146. }
  147.  
  148. /**
  149.  * Gets a result row as an enumerated array
  150.  *
  151.  * @param      resource  $result     result resource from dp_db_query()
  152.  * @return     mixed     numerical array of strings with row data, FALSE if
  153.  *                        there are no more rows
  154.  * @see        http://www.php.net/mysql_fetch_row, dp_db_connect, dp_db_query,
  155.  *              dp_db_fetch_one, dp_db_quote, dp_db_num_rows, dp_db_free
  156.  */
  157. function &dp_db_fetch_row($result)
  158. {
  159.     $row mysql_fetch_row($result);
  160.     return $row;
  161. }
  162.  
  163. /**
  164.  * Escapes special characters in a string for use in a SQL statement
  165.  *
  166.  * Escapes the value given in the first argument. All other arguments are
  167.  * ignored and only used, when given, by the MDB2 equivalent function.
  168.  *
  169.  * @param      string    $val        string that is to be escaped
  170.  * @return     mixed     escaped string, or FALSE on error
  171.  * @see        http://www.php.net/mysql_real_escape_string, dp_db_query,
  172.  *              dp_db_exec
  173.  */
  174. function dp_db_quote($val$type NULL$quote TRUE$escWildcards FALSE)
  175. {
  176.     if (!($link &dp_db_connect())
  177.             || FALSE === ($val mysql_real_escape_string($val$link))) {
  178.         return FALSE;
  179.     }
  180.  
  181.     return "'" $val "'";
  182. }
  183.  
  184. /**
  185.  * Gets number of rows in result
  186.  *
  187.  * Prints an error message to the server output in case of an error
  188.  *
  189.  * @param      resource  $result     result resource from dp_db_query()
  190.  * @return     mixed     number of rows in a result set, or FALSE on failure
  191.  * @see        http://www.php.net/mysql_num_rows, dp_db_query
  192.  */
  193. function dp_db_num_rows($result)
  194. {
  195.     $num_rows mysql_num_rows($result);
  196.     if (FALSE === $num_rows{
  197.         echo "dp_db_num_rows: Failed to get number of rows\n";
  198.     }
  199.  
  200.     return $num_rows;
  201. }
  202.  
  203. /**
  204.  * Gets the next primary ID for an INSERT
  205.  *
  206.  * Prints an error message to the server output in case of an error
  207.  *
  208.  * @param      string    $table      table name
  209.  * @param      string    $idColumn   field name
  210.  * @return     mixed     integer with next id, or FALSE on failure
  211.  * @see        dp_db_exec
  212.  */
  213. function dp_db_next_id($table$idColumn)
  214. {
  215.     if (!($link &dp_db_connect())) {
  216.         return FALSE;
  217.     }
  218.  
  219.     $result mysql_query($sql "SELECT MAX($idColumn) FROM $table"$link);
  220.     if (!$result{
  221.         echo mysql_error($link' [error number ' mysql_errno($link)
  222.             . "]\n";
  223.         return FALSE;
  224.     }
  225.     $max_id mysql_result($result00);
  226.     if (FALSE === $max_id{
  227.         echo "dp_db_fetch_one: Failed to fetch next id\n";
  228.         return FALSE;
  229.     }
  230.     mysql_free_result($result);
  231.  
  232.     return $max_id 1;
  233. }
  234.  
  235. /**
  236.  * Frees result memory
  237.  *
  238.  * @param      resource  $result     result resource from dp_db_query()
  239.  * @return     boolean   TRUE on success or FALSE on failure
  240.  * @see        http://www.php.net/mysql_free_result, dp_db_query,
  241.  *              dp_db_fetch_one, dp_db_fetch_row
  242.  */
  243. function dp_db_free($result)
  244. {
  245.     return is_resource($result&& mysql_free_result($result);
  246. }
  247. ?>

Documentation generated on Mon, 03 Sep 2007 22:19:10 +0200 by phpDocumentor 1.3.0RC6

Click me!
Guest#216
 
 
 
  Go to Top
 
 
Input Field OptionsClose Input Field Go to Top
 
Legal Notices | Copyright © 2006, 2007 Lennert Stock. All rights reserved. Last update: Mon Sep 03 2007, 21:50 CET.