Location: PHPKode > scripts > Query Paging > Connection.class.php
<?php
/**
 * @author Michele Andreoli <hide@address.com>
 * @name Connection.class.php
 * @version 0.3 updated: 17-03-2010
 * @license http://opensource.org/licenses/gpl-license-php GNU Public License
 * @package DBConnection
 */

class Connection {
   private $dbhost;
   private $dbusername;
   private $dbpassword;
   private $dbname;
   private $db;
   private $result;
   private $numRows;

   /**
    * Parameters for your database connection
    * @param <String> $host
    * @param <String> $user
    * @param <String> $password
    * @param <String> $dbname
    */
   public function __construct($host, $user, $password, $dbname) {
      $this->dbhost = $host;
      $this->dbusername = $user;
      $this->dbpassword = $password;
      $this->dbname = $dbname;
   }

   /**
    * Open the database connection and select the database specified in the constructor
    * @return <MySQL> MySQL link identifier
    */
   public function openDB() {
      $this->db = @mysql_connect($this->dbhost, $this->dbusername, $this->dbpassword) or die("Connection to database failed<br/>".mysql_error());

      mysql_select_db($this->dbname) or die("Selected database not exists<br/>".mysql_error($this->db));

      return $this->db;
   }

   /**
    * Close the database connection
    */
   public function closeDB() {
      mysql_close($this->db);
   }

   /**
    * Methods for SQL queries of type: SELECT
    * @param <String> $query
    * @param <Boolean> $unique to set true if the query returns only one row
    * @return <Array> return 0 or null if the query result is null or return the array result
    */
   public function query($query, $unique=false) {
      $this->result = @mysql_query($query, $this->db) or die("Query error<br/>".mysql_error($this->db));

      $this->numRows = mysql_num_rows($this->result);

      if ($unique == null || $unique == false) {
          if ($this->numRows > 0) {
             for($i = 0; $i < $this->numRows; $i++)
                $result[] = mysql_fetch_assoc($this->result);
             return $result;
          }
          else
             return array();
      }
      else if ($unique == true) {
          if ($this->numRows == 1) {
             return mysql_fetch_assoc($this->result);
          }
          else
             return null;
      }
      else
          return false;
   }

   /**
    * Return the number of rows for the selection queries
    * @return <int> number of rows of result
    */
   public function getNumRows() {
      return $this->numRows;
   }

   /**
    * Methods for SQL queries of type: UPDATE, INSERT, DELETE, etc...
    * @param <String> $query
    */
   public function modify($query) {
      $this->result = @mysql_query($query, $this->db) or die("Query error<br/>".mysql_error($this->db));
   }
}
?>
Return current item: Query Paging