WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
PHP Web Logs (BLogs)
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Submit Site
Forex Trading Online forex trading platform

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : PostGreSQL and MySQL 2 in 1 db Manager
Categories : PHP, PHP Classes, Databases, PostgreSQL, MySQL Click here to Update Your Picture
leaping langoor
Date : Aug 29th 2004
Grade : 2 of 5 (graded 4 times)
Viewed : 5572
File : No file for this code example.
Images : No Images for this code example.
Search : More code by leaping langoor
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

Ok so in this example, I'm showing you a class that is mixture of PostGreSQL and MYSQL. It's a combined class, and all the functions are contemporary to both the dbs( Escept the last function .. get_lastid() loyal to PostGreSQL ). As usual, this class is followed by an example.
sql.php:




class database

author...: leapinglangoor [ leapinglangoor@yahoo.co.in ]
version..: v.17
Updated..: 12 Aug 2004

Note.....: This script is 2 in 1 Portsgre cum MySQL
script that can be used just like any other
database manager.

CLASS DATABASE COMES WITHOUT ANY WARRANTY
USE AT YOUR OWN RISK

<?php
class SQL
{
  var
$type;
  var
$dbconn;
  var
$host, $port, $dbname, $user;
  var
$TYPE_PGSQL, $TYPE_MYSQL;
 
  function
Database( $type )
  {
   
$this->TYPE_PGSQL = "pgsql";
   
$this->TYPE_MYSQL = "mysql";

  }

  function
create( $type )
  {
   
$this->type = $type;
  }
 
  function
open( $host, $port, $dbname, $user, $password )
  {
   
$this->host = $host;
   
$this->port = $port;
   
$this->dbname = $dbname;
   
$this->user = $user;

    if ( !
strcmp( $this->type, $this->TYPE_PGSQL ) )
     
$this->dbconn = pg_pconnect( "host=$host port=$port dbname=$dbname user=$user password=$password" );
    else if ( !
strcmp( $this->type, $this->TYPE_MYSQL ) )
     
$this->dbconn = mysql_pconnect( "$host:$port", $user, $password );
   
    if ( !
$this->dbconn ) return false;
    else return
true;
  }
 
  function
close()
  {
    if ( !
strcmp( $this->type, $this->TYPE_PGSQL ) )
      return
pg_close( $this->dbconn );
    else if ( !
strcmp( $this->type, $this->TYPE_MYSQL ) )
      return
mysql_close($this->dbconn);
  }
 
  function
exec( $sql )
  {
    if ( !
strcmp( $this->type, $this->TYPE_PGSQL ) )
      return
pg_exec( $this->dbconn, $sql );
    else if ( !
strcmp( $this->type, $this->TYPE_MYSQL ) )
      return
mysql_db_query( $this->dbname, $sql, $this->dbconn );
  }
 
  function
numrows( $query )
  {
    if ( !
strcmp( $this->type, $this->TYPE_PGSQL ) )
      return
pg_numrows( $query );
    else if ( !
strcmp( $this->type, $this->TYPE_MYSQL ) )
      return
mysql_num_rows( $query );
  }
 
  function
fetch_array( $query, $row )
  {
    if ( !
strcmp( $this->type, $this->TYPE_PGSQL ) )
      return
pg_fetch_array( $query, $row );
    else if ( !
strcmp( $this->type, $this->TYPE_MYSQL ) )
    {
     
mysql_data_seek( $query, $row );
      return
mysql_fetch_array( $query );
    }
  }

  function
fetch_object( $query, $row )
  {
    if ( !
strcmp( $this->type, $this->TYPE_PGSQL ) )
      return
pg_fetch_object( $query, $row );
    else if ( !
strcmp( $this->type, $this->TYPE_MYSQL ) )
    {
     
mysql_data_seek( $query, $row );
      return
mysql_fetch_object( $query );
    }
  }
 
  function
get_lastid( $query, $table, $field )
  {
    if ( !
strcmp( $this->type, $this->TYPE_PGSQL ) )
    {
     
$oid = pg_getlastoid( $query );
     
$qaux = pg_exec( "select $field from $table where oid=$oid" );
     
$arr = pg_fetch_array( $qaux, 0 );
      return
$arr[$field];
    }
  }
}
?>



Here is the example to go with the class...


example.php
<?php

include('sql.php');

// Create an instance of the class.
$db = new SQL();

/*
@Param - $type
$type should either be
'pgsql' for PosGreSQL or
'myssql' for MYSQL
There wont be any diffrence in the methods of accessing data depending on the type of db chosen. Exception in the case of the last function - get_lastid();
*/

$type = 'pgsql';
$db->create( $type );

// Lets open a connection to the db
$db->open( 'localhost', 3306, 'database-to-open', 'user', 'password' );

$sql = 'SELECT * FROM `some_table`';
$result = $db->exec( $sql );




MySQL Handler
Categories : PHP, Databases, MySQL, Classes and Objects, PHP Classes
Powerful php/mysql Pagination for up to 6 URL Params
Categories : PHP, PHP Classes, Databases, MySQL, Navigation
Password reminder
Categories : PHP, PHP Classes, Databases, MySQL, Mail
MySQL Class to ease Database connectivity
Categories : MySQL, PHP Classes, Databases, PHP
usercounter class
Categories : PHP, PHP Classes, Databases, MySQL, Environment Variables
Simple Mini Poll class library (SimPoll)
Categories : PHP, PHP Classes, Databases, MySQL, Complete Programs
Simple database class
Categories : PHP, PHP Classes, MySQL, Databases
Simple usersOnline class - keep track of how many users are online on your site
Categories : PHP, PHP Classes, Databases, MySQL
Online Automatic Class Generator for MySQL Tables
Categories : PHP, PHP Classes, Classes and Objects, Databases, MySQL
DBE - Database Expander: Edit PostgreSQL individual database tables online via your Web browser!
Categories : PostgreSQL, Complete Programs, Databases, PHP Classes, PHP
Specify your connection settings and create a link to a MySQL database.
Categories : PHP, PHP Classes, Databases, MySQL, Beginner Guides
Setting up InnoDB on MySQL and using Transactions Begin, Commit, Rollback in PHP.
Categories : PHP Classes, Databases, PHP, MySQL, InnoDB
Ajax PHP Tree (Left and Right) with MySQL
Categories : PHP, Databases, MySQL, AJAX, PHP Classes
YellowPages Content Grabber (PHP5 +)
Categories : PHP, PHP Classes, Regexps, Databases, MySQL
MySQL Connection/Query Class
Categories : Databases, MySQL, PHP, PHP Classes