Quick Search:
Oracle PL/SQL Code Library
Find Or Post Oracle Jobs
Oracle Discussion & Chat
 PHP Code: Simple DB connection class Jump to:  
Category: >> PHP Code >> Simple DB connection class Bookmark and Share

<< lastnext >>

Snippet Name: Simple DB connection class

Description: A simple DB connection class with all often used mySQL functions.

Comment: (none)

Language: PHP, MYSQL
Highlight Mode: PHP
Last Modified: March 01st, 2009

CLASS DB_Class {
     VAR $db;
     FUNCTION DB_Class($dbname, $username, $password) {
          $this->db = MYSQL_CONNECT ('localhost', $username, $password)
           or DIE ("Unable to connect to Database Server");
          MYSQL_SELECT_DB ($dbname, $this->db) or DIE ("Could not select database");
     FUNCTION query($sql) {
          $result = MYSQL_QUERY ($sql, $this->db) or DIE ("Invalid query: " . MYSQL_ERROR());
          RETURN $result;
     FUNCTION fetch($sql) {
          $data = ARRAY();
          $result = $this->query($sql);
          WHILE($row = MYSQL_FETCH_ASSOC($result)) {
               $data[] = $row;
               RETURN $data;
     FUNCTION getone($sql) {
     $result = $this->query($sql);
     IF(MYSQL_NUM_ROWS($result) == 0)
          $value = FALSE;
          $value = MYSQL_RESULT($result, 0);
          RETURN $value;
// usage example....
$dbconnect = NEW DB_Class('table', 'user', 'password');
$query = "SELECT user_id FROM user_table WHERE $match = $search ORDER BY user_id DESC";
$result = $dbconnect->fetch($query);

Oracle Magazine
and Oracle White Papers

SQL University.net courses meet the most demanding needs of the business world for advanced education in a cost-effective manner. SQL University.net courses are available immediately for IT professionals and can be taken without disruption of your workplace schedule or processes.

Compared to traditional travel-based training, SQL University.net saves time and valuable corporate resources, allowing companies to do more with less. That's our mission, and that's what we deliver.

Click here to find out more
Home      :      Code Library      :      Sponsors      :      Privacy      :      Terms of Use      :      Contact Us 27 users online    © 2009 psoug.org

Forgot your password?