Location: PHPKode > projects > NedEngine > nedengine-0.4/includes/db/mysql.class.php
<?php
/*****************************************************************************
* NedEngine (c) René Gabriëls 2002                                           *
* NedEngine (c) Michiel van den Berg 2004                                    *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^                                           *
* This program is free software. You can redistribute it and/or modify       *
* it under the terms of the GNU General Public License as published by       *
* the Free Software Foundation; either version 2 of the License.             *
* -------------------------------------------------------------------------- *
* Database abstraction layer for MySQL                                       *
*****************************************************************************/

class _db {
  
  //  Database server
  var $server;
  
  //  Database name
  var $name;
  
  //  Database user
  var $user;
  
  //  Database password of $user
  var $password;
  
  //  Resource ID of the opened connection
  var $connection;
  
  //  Number of executed queries per page
  var $query_count = 0;
  
  //  Contains the result of the last query
  var $query_result;
  
  /*************************************************************************
  * Constructor: connect to the database                                   *
  *************************************************************************/
  function _db ($db_server, $db_name, $db_user, $db_password) {
    
    // Set database vars
    $this->server   = $db_server;
    $this->name     = $db_name;
    $this->user     = $db_user;
    $this->password = $db_password;
    
    // Connect to the database
    $this->connect ();
    
  }
  
  /*************************************************************************
  * Connect to the database and displey an error when it fails             *
  *************************************************************************/
  function connect () {
    
    // Generate die message
    $die_msg = str_replace ("%h", $this->server, _LANG_ERR_DATABASE_CONNECT_);
    
    // Connect to the database
    $this->connection = @mysql_pconnect ($this->server, $this->user, $this->password)
                        or die ($die_msg);
    
    // If connection available and database specified -> select it
    if ($this->connection) {
      
      // Generate die message
      $die_msg = str_replace ("%h", $this->server, _LANG_ERR_DATABASE_SELECT_);
      $die_msg = str_replace ("%n", $this->name, $die_msg);
      
      // Select the specified database
      @mysql_select_db ($this->name) or die ($die_msg);
      
    }
    
  }
	
  /*************************************************************************
  * Execute a query on the database through the $conntection ID            *
  *************************************************************************/
  function query ($query) {
      
    global $debug_mode;
    
    // When we are in debug mode -> display the specified query
    if ($debug_mode == 1) {
      echo "<br>================<br>\n";
      echo nl2br (str_replace (" ", "&nbsp;", $query));
      echo "<br>================<br>\n";
    }
    
    // Execute query
    if ($query != "") {
      unset ($this->query_result);
      $this->query_count++;
      $this->query_result = @mysql_query ($query, $this->connection);
    }
    
    // Check if the query has been executed and show a message in debug mode
    if ($debug_mode == 1 && !isset ($this->query_result)) {
      echo "<span class='texterror'>" . _LANG_ERR_DATABASE_QUERY_ . "</span><br>";
      return false;
    }
    
    // Return result identifier
    return $this->query_result;
    
  }
  
  /*************************************************************************
  * Return one row of the resultset (where the pointer is)                 *
  *************************************************************************/
  function fetch_array () {
    
    if ($this->query_result) {
      return @mysql_fetch_array ($this->query_result);
    }
    else {
      return false;
    }
    
  }
  
  
  /*************************************************************************
  * Fetches all data and puts it into an array $data                       *
  *************************************************************************/
  function fetch_all () {

    if ($this->query_result) {
      $data = array();

      for ($i = 0; $i < $this->num_rows(); $i++)
          $data[$i] = @mysql_fetch_array ($this->query_result);

      return $data;
    } else {
      return false;
    }
  }
  
  /*************************************************************************
  * Return the number of affected rows                                     *
  *************************************************************************/
  function affected_rows () {
    
    if ($this->connection) {
      $rows_affected = @mysql_affected_rows ($this->connection);
      return $rows_affected;
    }
    else {
      return false;
    }
    
  }
  
  /*************************************************************************
  * Return the rowcount of the last executed query                         *
  *************************************************************************/
  function num_rows () {
    
    if (isset ($this->query_result)) {
      return @mysql_num_rows ($this->query_result);
    }
    else {
      return 0;
    }
    
  }
  
  /*************************************************************************
  * Return the fieldcount of the last executed query                       *
  *************************************************************************/
  function num_fields () {
    
    if (isset ($this->query_result)) {
      return @mysql_num_fields ($this->result);
    }
    else {
      return false;
    }
  }
  
  /*************************************************************************
  * Return the fieldname of $field_id                                      *
  *************************************************************************/
  function field_name ($field_id) {
    
    if (isset ($this->query_result)) {
      return @mysql_field_name ($this->query_result, $field_id);
    }
    else {
      return false;
    }
  }
  
  /*************************************************************************
  * Return the type of $field_id                                           *
  *************************************************************************/
  function field_type ($field_id) {
    
    if (isset ($this->query_result)) {
      return @mysql_field_type ($this->query_result, $field_id);
    }
    else {
      return false;
    }
  }
  
  /*************************************************************************
  * Set result pointer to specified row                                    *
  *************************************************************************/
  function data_seek ($row) {
    
    if (isset ($this->query_result)) {
      return @mysql_data_seek ($this->query_result, $row);
    }
    else {
      return false;
    }
  }
  
  /*************************************************************************
  * Free resultset before end of script if much memory is used             *
  *************************************************************************/
  function free_result () {
    
    if (isset ($this->query_result)) {
      @mysql_free_result ($this->connection);
      return true;
    }
    else {
      return false;
    }
  }
}

?>
Return current item: NedEngine