[ Index ]
 

Code source de WebCalendar 1.0.5

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables | Statistiques

title

Body

[fermer]

/includes/ -> php-dbi.php (source)

   1  <?php
   2  /**
   3   * Generic database access.
   4   *
   5   * The functions defined in this file are meant to provide a single API to the
   6   * different PHP database APIs.  Unfortunately, this is necessary since PHP
   7   * does not yet have a common db API.  The value of
   8   * <var>$GLOBALS["db_type"]</var> should be defined somewhere to one of the
   9   * following:
  10   * - mysql
  11   * - mssql
  12   * - oracle  (This uses the Oracle8 OCI API, so Oracle 8 libs are required)
  13   * - postgresl
  14   * - odbc
  15   * - ibase (Interbase)
  16   *
  17   * <b>Limitations:</b>
  18   *
  19   * - This assumes a single connection to a single database for the sake of
  20   *   simplicity.  Do not make a new connection until you are completely
  21   *   finished with the previous one.  However, you can execute more than query
  22   *   at the same time.
  23   * - Rather than use the associative arrays returned with xxx_fetch_array(),
  24   *   normal arrays are used with xxx_fetch_row().  (Some db APIs don't support
  25   *   xxx_fetch_array().)
  26   *
  27   * @author Craig Knudsen <cknudsen@cknudsen.com>
  28   * @copyright Craig Knudsen, <cknudsen@cknudsen.com>, http://www.k5n.us/cknudsen
  29   * @license http://www.gnu.org/licenses/gpl.html GNU GPL
  30   * @package WebCalendar
  31   *
  32   * History:
  33   * 09-Dec-2005  Craig Knudsen
  34   *    Added DB2 support (patch from Helmut Tessarek)
  35   * 17-Mar-2005  Ray Jones
  36   *     Changed mssql_error to mssql_get_last_message
  37   * 23-Jan-2005  Craig Knudsen <cknudsen@cknudsen.com>
  38   *     Added documentation to be used with php2html.pl
  39   * 19-Jan-2005  Craig Knudsen <cknudsen@cknudsen.com>
  40   *     Add option for verbose error messages.
  41   * 19-Jan-2004  Craig Knudsen <cknudsen@cknudsen.com>
  42   *     Added mssql support
  43   *     Code from raspail@users.sourceforge.net
  44   * 02-Jul-2004  Craig Knudsen <cknudsen@cknudsen.com>
  45   *     Added mysqli support
  46   *     Code from Francesco Riosa
  47   * 31-May-2002  Craig Knudsen <cknudsen@radix.net>
  48   *     Added support for Interbase contributed by
  49   *     Marco Forlin
  50   * 11-Jul-2001  Craig Knudsen <cknudsen@radix.net>
  51   *     Removed pass by reference for odbc_fetch_into()
  52   *     Removed ++ in call to pg_fetch_array()
  53   * 22-Apr-2000  Ken Harris <kharris@lhinfo.com>
  54   *     PostgreSQL fixes
  55   * 23-Feb-2000  Craig Knudsen <cknudsen@radix.net>
  56   *     Initial release
  57   */
  58  
  59  if ( empty ( $PHP_SELF ) && ! empty ( $_SERVER ) &&
  60    ! empty ( $_SERVER['PHP_SELF'] ) ) {
  61    $PHP_SELF = $_SERVER['PHP_SELF'];
  62  }
  63  if ( ! empty ( $PHP_SELF ) && preg_match ( "/\/includes\//", $PHP_SELF ) ) {
  64      die ( "You can't access this file directly!" );
  65  }
  66  
  67  
  68  // Enable the following to show the actual database error in the browser.
  69  // It is more secure to not show this info, so this should only be turned
  70  // on for debugging purposes.
  71  $phpdbiVerbose = false;
  72  
  73  /**
  74   * Opens up a database connection.
  75   *
  76   * Use a pooled connection if the db supports it and
  77   * the <var>db_persistent</var> setting is enabled.
  78   *
  79   * <b>Notes:</b>
  80   * - The database type is determined by the global variable
  81   *   <var>db_type</var>
  82   * - For ODBC, <var>$host</var> is ignored, <var>$database</var> = DSN
  83   * - For Oracle, <var>$database</var> = tnsnames name
  84   * - Use the {@link dbi_error()} function to get error information if the connection
  85   *   fails
  86   *
  87   * @param string $host     Hostname of database server
  88   * @param string $login    Database login
  89   * @param string $password Database login password
  90   * @param string $database Name of database
  91   * 
  92   * @return resource The connection
  93   */
  94  function dbi_connect ( $host, $login, $password, $database ) {
  95    if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
  96      if ($GLOBALS["db_persistent"]) {
  97        $c = mysql_pconnect ( $host, $login, $password );
  98      } else {
  99        $c = mysql_connect ( $host, $login, $password );
 100      }
 101      if ( $c ) {
 102        if ( ! mysql_select_db ( $database ) )
 103          return false;
 104        return $c;
 105      } else {
 106        return false;
 107      }
 108    } else if ( strcmp ( $GLOBALS["db_type"], "mysqli" ) == 0 ) {
 109      if ($GLOBALS["db_persistent"]) {
 110        $c = @mysqli_connect ( $host, $login, $password, $database);
 111      } else {
 112        $c = @mysqli_connect ( $host, $login, $password, $database);
 113      }
 114      if ( $c ) {
 115        /*
 116        if ( ! mysqli_select_db ( $c, $database ) )
 117          return false;
 118        */
 119        $GLOBALS["db_connection"] = $c;
 120        return $c;
 121      } else {
 122        return false;
 123      }
 124    } else if ( strcmp ( $GLOBALS["db_type"], "mssql" ) == 0 ) {
 125      if ($GLOBALS["db_persistent"]) {
 126        $c = mssql_pconnect ( $host, $login, $password );
 127      } else {
 128        $c = mssql_connect ( $host, $login, $password );
 129      }
 130      if ( $c ) {
 131        if ( ! mssql_select_db ( $database ) )
 132          return false;
 133        return $c;
 134      } else {
 135        return false;
 136      }
 137    } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
 138      if ( strlen ( $host ) && strcmp ( $host, "localhost" ) )
 139        $c = OCIPLogon ( "$login@$host", $password, $database );
 140      else
 141        $c = OCIPLogon ( $login, $password, $database );
 142      $GLOBALS["oracle_connection"] = $c;
 143      return $c;
 144    } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
 145      if ( strlen ( $password ) ) {
 146        if ( strlen ( $host ) ) {
 147          $dbargs = "host=$host dbname=$database user=$login password=$password";
 148        } else {
 149          $dbargs = "dbname=$database user=$login password=$password";
 150        }
 151      } else {
 152        if ( strlen ( $host ) ) {
 153          $dbargs = "host=$host dbname=$database user=$login";
 154        } else {
 155          $dbargs = "dbname=$database user=$login";
 156        }
 157      }
 158      if ($GLOBALS["db_persistent"]) {
 159        $c = pg_pconnect ( $dbargs );
 160      } else {
 161        $c = pg_connect ( $dbargs );
 162      }
 163      $GLOBALS["postgresql_connection"] = $c;
 164      if ( ! $c ) {
 165          echo "Error connecting to database\n";
 166          exit;
 167      }
 168      return $c;
 169    } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
 170      if ($GLOBALS["db_persistent"]) {
 171        $c = odbc_pconnect ( $database, $login, $password );
 172      } else {
 173        $c = odbc_connect ( $database, $login, $password );
 174      }
 175      $GLOBALS["odbc_connection"] = $c;
 176      return $c;
 177    } else if ( strcmp ( $GLOBALS["db_type"], "ibm_db2" ) == 0 ) {
 178      if ($GLOBALS["db_persistent"]) {
 179        $c = db2_pconnect ( $database, $login, $password );
 180      } else {
 181        $c = db2_connect ( $database, $login, $password );
 182      }
 183      $GLOBALS["ibm_db2_connection"] = $c;
 184      return $c;
 185    } else if ( strcmp ( $GLOBALS["db_type"], "ibase" ) == 0 ) {
 186      $host = $host . ":" . $database;
 187      if ($GLOBALS["db_persistent"]) {
 188        $c = ibase_pconnect ( $host, $login, $password );
 189      } else {
 190        $c = ibase_connect ( $host, $login, $password );
 191      }
 192      return $c;
 193    } else {
 194      if ( empty ( $GLOBALS["db_type"] ) )
 195        dbi_fatal_error ( "dbi_connect(): db_type not defined." );
 196      else
 197        dbi_fatal_error ( "dbi_connect(): invalid db_type '" .
 198          $GLOBALS["db_type"] . "'" );
 199    }
 200  }
 201  
 202  /**
 203   * Closes a database connection.
 204   *
 205   * This is not necessary for any database that uses pooled connections such as
 206   * MySQL, but a good programming practice.
 207   *
 208   * @param resource $conn The database connection
 209   *
 210   * @return bool True on success, false on error
 211   */
 212  function dbi_close ( $conn ) {
 213    if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
 214      return mysql_close ( $conn );
 215    } else if ( strcmp ( $GLOBALS["db_type"], "mysqli" ) == 0 ) {
 216      return mysqli_close ( $conn );
 217    } else if ( strcmp ( $GLOBALS["db_type"], "mssql" ) == 0 ) {
 218      return mssql_close ( $conn );
 219    } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
 220      return OCILogOff ( $conn );
 221    } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
 222      return pg_close ( $GLOBALS["postgresql_connection"] );
 223    } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
 224      return odbc_close ( $GLOBALS["odbc_connection"] );
 225    } else if ( strcmp ( $GLOBALS["db_type"], "ibm_db2" ) == 0 ) {
 226      return db2_close ( $GLOBALS["ibm_db2_connection"] );
 227    } else if ( strcmp ( $GLOBALS["db_type"], "ibase" ) == 0 ) {
 228      return ibase_close ( $conn );
 229    } else {
 230      dbi_fatal_error ( "dbi_close(): db_type not defined." );
 231    }
 232  }
 233  
 234  // Select the database that all queries should use
 235  //function dbi_select_db ( $database ) {
 236  //  if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
 237  //    return mysql_select_db ( $database );
 238  //  } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
 239  //    // Not supported.  Must sent up a tnsname and user that uses
 240  //    // the correct tablesapce.
 241  //    return true;
 242  //  } else {
 243  //    dbi_fatal_error ( "dbi_select_db(): db_type not defined." );
 244  //  }
 245  //}
 246  
 247  /**
 248   * Executes a SQL query.
 249   *
 250   * <b>Note:</b> Use the {@link dbi_error()} function to get error information
 251   * if the connection fails.
 252   *
 253   * @param string $sql          SQL of query to execute
 254   * @param bool   $fatalOnError Abort execution if there is a database error?
 255   * @param bool   $showError    Display error to user (including possibly the
 256   *                             SQL) if there is a database error?
 257   *
 258   * @return mixed The query result resource on queries (which can then be
 259   *               passed to the {@link dbi_fetch_row()} function to obtain the
 260   *               results), or true/false on insert or delete queries.
 261   */
 262  function dbi_query ( $sql, $fatalOnError=true, $showError=true ) {
 263    global $phpdbiVerbose;
 264    if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
 265      $res = mysql_query ( $sql );
 266      if ( ! $res )
 267        dbi_fatal_error ( "Error executing query." .
 268          $phpdbiVerbose ? ( dbi_error() . "\n\n<br />\n" . $sql ) : "" .
 269          "", $fatalOnError, $showError );
 270      return $res;
 271    } else if ( strcmp ( $GLOBALS["db_type"], "mysqli" ) == 0 ) {
 272      $res = mysqli_query ( $GLOBALS["db_connection"], $sql );
 273      if ( ! $res )
 274        dbi_fatal_error ( "Error executing query." .
 275          $phpdbiVerbose ? ( dbi_error() . "\n\n<br />\n" . $sql ) : "" .
 276          "", $fatalOnError, $showError );
 277      return $res;
 278    } else if ( strcmp ( $GLOBALS["db_type"], "mssql" ) == 0 ) {
 279      $res = mssql_query ( $sql );
 280      if ( ! $res )
 281        dbi_fatal_error ( "Error executing query." .
 282          $phpdbiVerbose ? ( dbi_error() . "\n\n<br />\n" . $sql ) : "" .
 283          "", $fatalOnError, $showError );
 284      return $res;
 285    } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
 286      $GLOBALS["oracle_statement"] =
 287        OCIParse ( $GLOBALS["oracle_connection"], $sql );
 288      return OCIExecute ( $GLOBALS["oracle_statement"],
 289        OCI_COMMIT_ON_SUCCESS );
 290    } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
 291      $res =  pg_exec ( $GLOBALS["postgresql_connection"], $sql );
 292      $GLOBALS["postgresql_row[\"$res\"]"] = 0;
 293      if ( ! $res )
 294        dbi_fatal_error ( "Error executing query." .
 295          $phpdbiVerbose ? ( dbi_error() . "\n\n<br />\n" . $sql ) : "" .
 296          "", $fatalOnError, $showError );
 297      $GLOBALS["postgresql_numrows[\"$res\"]"] = pg_numrows ( $res );
 298      return $res;
 299    } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
 300      return odbc_exec ( $GLOBALS["odbc_connection"], $sql );
 301    } else if ( strcmp ( $GLOBALS["db_type"], "ibm_db2" ) == 0 ) {
 302      $res = db2_exec ( $GLOBALS["ibm_db2_connection"], $sql );
 303      if ( ! $res )
 304        dbi_fatal_error ( "Error executing query." .
 305          $phpdbiVerbose ? ( dbi_error() . "\n\n<br />\n" . $sql ) : "" .
 306          "", $fatalOnError, $showError );
 307      return $res;
 308    } else if ( strcmp ( $GLOBALS["db_type"], "ibase" ) == 0 ) {
 309      $res = ibase_query ( $sql );
 310      if ( ! $res )
 311        dbi_fatal_error ( "Error executing query." .
 312          $phpdbiVerbose ? ( dbi_error() . "\n\n<br />\n" . $sql ) : "" .
 313          "", $fatalOnError, $showError );
 314      return $res;
 315    } else {
 316      dbi_fatal_error ( "dbi_query(): db_type not defined." );
 317    }
 318  }
 319  
 320  // Determine the number of rows from a result
 321  //function dbi_num_rows ( $res ) {
 322  //  if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
 323  //    return mysql_num_rows ( $res );
 324  //  } else {
 325  //    dbi_fatal_error ( "dbi_num_rows(): db_type not defined." );
 326  //  }
 327  //}
 328  
 329  /**
 330   * Retrieves a single row from the database and returns it as an array.
 331   *
 332   * <b>Note:</b> We don't use the more useful xxx_fetch_array because not all
 333   * databases support this function.
 334   *
 335   * <b>Note:</b> Use the {@link dbi_error()} function to get error information
 336   * if the connection fails.
 337   *
 338   * @param resource $res The database query resource returned from
 339   *                      the {@link dbi_query()} function.
 340   *
 341   * @return mixed An array of database columns representing a single row in
 342   *               the query result or false on an error.
 343   */
 344  function dbi_fetch_row ( $res ) {
 345    if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
 346      return mysql_fetch_array ( $res );
 347    } else if ( strcmp ( $GLOBALS["db_type"], "mysqli" ) == 0 ) {
 348      return mysqli_fetch_array ( $res );
 349    } else if ( strcmp ( $GLOBALS["db_type"], "mssql" ) == 0 ) {
 350      return mssql_fetch_array ( $res );
 351    } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
 352      if ( OCIFetchInto ( $GLOBALS["oracle_statement"], $row,
 353        OCI_NUM + OCI_RETURN_NULLS  ) )
 354        return $row;
 355      return 0;
 356    } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
 357      if ( $GLOBALS["postgresql_numrows[\"$res\"]"]  > $GLOBALS["postgresql_row[\"$res\"]"] ) {
 358          $r =  pg_fetch_array ( $res, $GLOBALS["postgresql_row[\"$res\"]"] );
 359          $GLOBALS["postgresql_row[\"$res\"]"]++;
 360          if ( ! $r ) {
 361              echo "Unable to fetch row\n";
 362              return '';
 363          }
 364      }
 365      else {
 366          $r = '';
 367      }
 368      return $r;
 369    } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
 370      if ( ! odbc_fetch_into ( $res, $ret ) )
 371        return false;
 372      return $ret;
 373    } else if ( strcmp ( $GLOBALS["db_type"], "ibm_db2" ) == 0 ) {
 374      return db2_fetch_array ( $res );
 375    } else if ( strcmp ( $GLOBALS["db_type"], "ibase" ) == 0 ) {
 376      return ibase_fetch_row ( $res );
 377    } else {
 378      dbi_fatal_error ( "dbi_fetch_row(): db_type not defined." );
 379    }
 380  }
 381  
 382  /**
 383   * Returns the number of rows affected by the last INSERT, UPDATE or DELETE.
 384   *
 385   * <b>Note:</b> Use the {@link dbi_error()} function to get error information
 386   * if the connection fails.
 387   *
 388   * @param resource $conn The database connection
 389   * @param resource $res  The database query resource returned from
 390   *                       the {@link dbi_query()} function.
 391   *
 392   * @return int The number or database rows affected.
 393   */
 394  function dbi_affected_rows ( $conn, $res ) {
 395    if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
 396      return mysql_affected_rows ( $conn );
 397    } else if ( strcmp ( $GLOBALS["db_type"], "mysqli" ) == 0 ) {
 398      return mysqli_affected_rows ( $conn );
 399    } else if ( strcmp ( $GLOBALS["db_type"], "mssql" ) == 0 ) {
 400      return mssql_affected_rows ( $conn );
 401    } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
 402      if ( $GLOBALS["oracle_statement"] >= 0 ) {
 403        return OCIRowCount ( $GLOBALS["oracle_statement"] );
 404      } else {
 405        return -1;
 406      }
 407    } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
 408      return pg_affected_rows ( $res );
 409    } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
 410      return odbc_num_rows ( $res );
 411    } else if ( strcmp ( $GLOBALS["db_type"], "ibm_db2" ) == 0 ) {
 412      return db2_num_rows ( $res );
 413    } else if ( strcmp ( $GLOBALS["db_type"], "ibase" ) == 0 ) {
 414      return ibase_affected_rows ( $conn );
 415    } else {
 416      dbi_fatal_error ( "dbi_free_result(): db_type not defined." );
 417    }
 418  }
 419  
 420  /**
 421    * Frees a result set.
 422    *
 423    * @param resource $res The database query resource returned from
 424    *                      the {@link dbi_query()} function.
 425    *
 426    * @return bool True on success
 427    */
 428  function dbi_free_result ( $res ) {
 429    if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
 430      return mysql_free_result ( $res );
 431    } else if ( strcmp ( $GLOBALS["db_type"], "mysqli" ) == 0 ) {
 432      return mysqli_free_result ( $res );
 433    } else if ( strcmp ( $GLOBALS["db_type"], "mssql" ) == 0 ) {
 434      return mssql_free_result ( $res );
 435    } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
 436      // Not supported.  Ingore.
 437      if ( $GLOBALS["oracle_statement"] >= 0 ) {
 438        OCIFreeStatement ( $GLOBALS["oracle_statement"] );
 439        $GLOBALS["oracle_statement"] = -1;
 440      }
 441    } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
 442      return pg_freeresult ( $res );
 443    } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
 444      return odbc_free_result ( $res );
 445    } else if ( strcmp ( $GLOBALS["db_type"], "ibm_db2" ) == 0 ) {
 446      return db2_free_result ( $res );
 447    } else if ( strcmp ( $GLOBALS["db_type"], "ibase" ) == 0 ) {
 448      return ibase_free_result ( $res );
 449    } else {
 450      dbi_fatal_error ( "dbi_free_result(): db_type not defined." );
 451    }
 452  }
 453  
 454  /**
 455   * Gets the latest database error message.
 456   *
 457   * @return string The text of the last database error.  (The type of
 458   *                information varies depending on the which type of database
 459   *                is being used.)
 460   */
 461  function dbi_error () {
 462    if ( strcmp ( $GLOBALS["db_type"], "mysql" ) == 0 ) {
 463      $ret = mysql_error ();
 464    } else if ( strcmp ( $GLOBALS["db_type"], "mysqli" ) == 0 ) {
 465      $ret = mysqli_error ($GLOBALS["db_connection"]);
 466    } else if ( strcmp ( $GLOBALS["db_type"], "mssql" ) == 0 ) {
 467      // no real mssql_error function. this is as good as it gets
 468      $ret = mssql_get_last_message ();
 469    } else if ( strcmp ( $GLOBALS["db_type"], "oracle" ) == 0 ) {
 470      $ret = OCIError ( $GLOBALS["oracle_connection"] );
 471    } else if ( strcmp ( $GLOBALS["db_type"], "postgresql" ) == 0 ) {
 472      $ret = pg_errormessage ( $GLOBALS["postgresql_connection"] );
 473    } else if ( strcmp ( $GLOBALS["db_type"], "odbc" ) == 0 ) {
 474      // no way to get error from ODBC API
 475      $ret = "Unknown ODBC error";
 476    } else if ( strcmp ( $GLOBALS["db_type"], "ibm_db2" ) == 0 ) {
 477      $ret = db2_conn_errormsg ();
 478      if ( $ret == '' )
 479         $ret = db2_stmt_errormsg ();
 480    } else if ( strcmp ( $GLOBALS["db_type"], "ibase" ) == 0 ) {
 481      $ret = ibase_errmsg ();
 482    } else {
 483      $ret = "dbi_error(): db_type not defined.";
 484    }
 485    if ( strlen ( $ret ) )
 486      return $ret;
 487    else
 488      return "Unknown error";
 489  }
 490  
 491  /**
 492   * Displays a fatal database error and aborts execution.
 493   *
 494   * @param string $msg       The database error message
 495   * @param bool   $doExit    Abort execution?
 496   * @param bool   $showError Show the details of the error (possibly including
 497   *                           the SQL that caused the error)?
 498   */
 499  function dbi_fatal_error ( $msg, $doExit=true, $showError=true ) {
 500    if ( $showError ) {
 501      echo "<h2>Error</h2>\n";
 502      echo "<!--begin_error(dbierror)-->\n";
 503      echo "$msg\n";
 504      echo "<!--end_error-->\n";
 505    }
 506    if ( $doExit )
 507      exit;
 508  }
 509  ?>


Généré le : Fri Nov 30 19:09:19 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics