SQL Classe (pgsql + mysql)
Auteur: Wim - 14 augustus 2007 - 18:15 - Gekeurd door: Stijn - Hits: 3197 - Aantal punten: (0 stemmen)
Dit script bevat 2 classes en 1 interface.
Interface: SQL
Classes: mysql, pgsql
Beide classes zijn net hetzelfde in gebruik, dus het overschakelen van pgsql naar mysql (of omgekeerd), is zeer simpel (enkel het aanspreken van een classe veranderen; new pgsql ipv new mysql (of omgekeerd).
Om te kijken welke functies alles bevat, is het waarschijnlijk het makkelijkst om naar de interface te kijken.
mysql_real_escape_string en pg_escape_string worden automatisch toegevoegd als je de functies select, insert of delete gebruikt.
DOCUMENTATIE: PDF - ODT (OpenOffice.org writer)
Veel plezier met de classes,
Wim Mariën
UPDATES:
16/08/2007: is_string() functie verwijderd bij de select-query's. Zo konden er namelijk geen integers (voor bvb een id) meegegeven worden voor de WHERE clausule bij een query!
Code:
sql.interface.php
<?php
/**
* @package SQL
* @author Wim Mariën (http://www.gdx.be/)
* @copyright (c) 2007 Wim Mariën - gdx.be
* @license http://www.opensource.org/licenses/gpl-license.php
*/
interface sql
{
public function __construct ($host=NULL, $user=NULL, $pass=NULL, $db=NULL, $port=NULL);
public function connect ($host, $user, $pass, $db, $port='');
public function query ($query);
public function select ($table, $fields=NULL, $wFields=NULL, $wValues=NULL, $order=NULL, $limit=NULL);
public function insert ($table, $name, $value);
public function delete ($table, $field=NULL, $value=NULL, $limit=NULL);
public function fetchobject ($query);
public function fetcharray ($query);
public function disconnect ();
public function __destruct ();
}
?>
<?php
/**
* @package SQL
* @author Wim Mariën (http://www.gdx.be/)
* @copyright (c) 2007 Wim Mariën - gdx.be
* @license http://www.opensource.org/licenses/gpl-license.php
*/
interface sql
{
public function __construct ( $host = NULL , $user = NULL , $pass = NULL , $db = NULL , $port = NULL ) ;
public function connect ( $host , $user , $pass , $db , $port = '' ) ;
public function query ( $query ) ;
public function select ( $table , $fields = NULL , $wFields = NULL , $wValues = NULL , $order = NULL , $limit = NULL ) ;
public function insert ( $table , $name , $value ) ;
public function delete ( $table , $field = NULL , $value = NULL , $limit = NULL ) ;
public function fetchobject ( $query ) ;
public function fetcharray ( $query ) ;
public function disconnect ( ) ;
public function __destruct ( ) ;
}
?>
mysql.class.php:
<?php
/**
* @package SQL
* @author Wim Mariën (http://www.gdx.be/)
* @copyright (c) 2007 Wim Mariën - gdx.be
* @license http://www.opensource.org/licenses/gpl-license.php
*/
class mysql implements sql
{
protected $sqlcon;
public $nQuerys = 0;
public $fetch;
public function __construct($host=NULL, $user=NULL, $pass=NULL, $db=NULL, $port=NULL)
{
if(isset($host) && isset($user) && isset($db))
{
$this->connect($host, $user, $pass, $db, $port);
}
}
public function connect ($host, $user, $pass, $db, $port='3306')
{
$this->sqlcon = mysql_connect($host.':'.$port, $user, $pass) or die('Could not connect to the database server:<br />'.mysql_error());
mysql_select_db($db, $this->sqlcon) or die('Could not select the database:<br />'.mysql_error());
return true;
}
public function query ($query)
{
$this->nQuerys++;
return mysql_query($query);
}
public function select ($table, $fields=NULL, $wFields=NULL, $wValues=NULL, $order=NULL, $limit=NULL)
{
if(is_array($fields))
{
$fields = implode(',',$fields);
}
$strQuery = 'SELECT '.$fields.' FROM '.$table;
if(is_array($wFields) && is_array($wValues) && count($wFields) == count($wValues))
{
$strQuery .= ' WHERE ';
for($i = 0; $i <= count($value)-1; $i++)
{
if($i != 0)
$strQuery .= ' AND ';
$strQuery .= $field[$i].'=\''.mysql_real_escape_string($value[$i]).'\'';
}
}
elseif(isset($wValues) && isset($wFields))
{
$strQuery .= ' WHERE '.$wFields.'=\''.mysql_real_escape_string($wValues).'\'';
}
if(isset($order))
{
$strQuery .= ' ORDER BY '.$order;
}
if(isset($limit) && is_numeric(str_replace(',', '', $limit)))
{
$strQuery .= ' LIMIT '.$limit;
}
return $this->query($strQuery);
}
public function insert ($table, $name, $value)
{
if(is_string($table) && is_array($name) && is_array($value) && count($name) == count($value))
{
$name = implode(',', $name);
$values = array();
foreach($value as $v)
{
$values[] = '\''.mysql_real_escape_string($v).'\'';
}
$value = implode(',', $values);
}
return $this->query('INSERT INTO '.$table.' ('.$name.') VALUES ('.$value.')');
}
public function delete ($table, $field=NULL, $value=NULL, $limit=NULL)
{
$strQuery = 'DELETE FROM '.$table;
if(is_string($field) && isset($value))
{
$strQuery .= ' WHERE '.$field.'=\''.mysql_real_escape_string($value).'\'';
}
elseif(is_array($field) && is_array($value) && count($field) == count($value))
{
$strQuery .= ' WHERE ';
for($i = 0; $i <= count($value)-1; $i++)
{
if($i != 0)
$strQuery .= ' AND ';
$strQuery .= $field[$i].'=\''.mysql_real_escape_string($value[$i]).'\'';
}
}
if(isset($limit) && is_numeric($limit))
{
$strQuery .= ' LIMIT '.$limit;
}
return $this->query($strQuery);
}
public function fetchobject ($query)
{
return mysql_fetch_object($query);
}
public function fetcharray ($query)
{
return mysql_fetch_array($query);
}
public function disconnect ()
{
@mysql_close($this->sqlcon);
}
public function __destruct ()
{
$this->disconnect();
}
}
?>
<?php
/**
* @package SQL
* @author Wim Mariën (http://www.gdx.be/)
* @copyright (c) 2007 Wim Mariën - gdx.be
* @license http://www.opensource.org/licenses/gpl-license.php
*/
class mysql implements sql
{
protected $sqlcon ;
public $nQuerys = 0 ;
public $fetch ;
public function __construct( $host = NULL , $user = NULL , $pass = NULL , $db = NULL , $port = NULL )
{
{
$this -> connect ( $host , $user , $pass , $db , $port ) ;
}
}
public function connect ( $host , $user , $pass , $db , $port = '3306' )
{
return true ;
}
public function query ( $query )
{
$this -> nQuerys ++;
}
public function select ( $table , $fields = NULL , $wFields = NULL , $wValues = NULL , $order = NULL , $limit = NULL )
{
{
}
$strQuery = 'SELECT ' . $fields . ' FROM ' . $table ;
{
$strQuery .= ' WHERE ' ;
for ( $i = 0 ; $i <= count ( $value ) - 1 ; $i ++ ) {
if ( $i != 0 )
$strQuery .= ' AND ' ;
$strQuery .= $field [ $i ] . '=\' '.mysql_real_escape_string($value[$i]).' \'' ;
}
}
{
$strQuery .= ' WHERE ' . $wFields . '=\' '.mysql_real_escape_string($wValues).' \'' ;
}
{
$strQuery .= ' ORDER BY ' . $order ;
}
{
$strQuery .= ' LIMIT ' . $limit ;
}
return $this -> query ( $strQuery ) ;
}
public function insert ( $table , $name , $value )
{
{
foreach ( $value as $v )
{
$values [ ] = '\' '.mysql_real_escape_string($v).' \'' ;
}
}
return $this -> query ( 'INSERT INTO ' . $table . ' (' . $name . ') VALUES (' . $value . ')' ) ;
}
public function delete ( $table , $field = NULL , $value = NULL , $limit = NULL )
{
$strQuery = 'DELETE FROM ' . $table ;
{
$strQuery .= ' WHERE ' . $field . '=\' '.mysql_real_escape_string($value).' \'' ;
}
{
$strQuery .= ' WHERE ' ;
for ( $i = 0 ; $i <= count ( $value ) - 1 ; $i ++ ) {
if ( $i != 0 )
$strQuery .= ' AND ' ;
$strQuery .= $field [ $i ] . '=\' '.mysql_real_escape_string($value[$i]).' \'' ;
}
}
{
$strQuery .= ' LIMIT ' . $limit ;
}
return $this -> query ( $strQuery ) ;
}
public function fetchobject ( $query )
{
}
public function fetcharray ( $query )
{
}
public function disconnect ( )
{
}
public function __destruct ( )
{
$this -> disconnect ( ) ;
}
}
?>
pgsql.class.php:
<?php
/**
* @package SQL
* @author Wim Mariën (http://www.gdx.be/)
* @copyright (c) 2007 Wim Mariën - gdx.be
* @license http://www.opensource.org/licenses/gpl-license.php
*/
class pgsql implements sql
{
protected $sqlcon;
public $nQuerys = 0;
public $fetch;
public function __construct($host=NULL, $user=NULL, $pass=NULL, $db=NULL, $port=NULL)
{
if(isset($host) && isset($user) && isset($db))
{
$this->connect($host, $user, $pass, $db, $port);
}
}
public function connect ($host, $user, $pass, $db, $port='5432')
{
$pass = (($pass == NULL) ? '' : ' password='.$pass);
$this->sqlcon = pg_connect('host='.$host.' user='.$user.' port='.$port.' dbname='.$db.' '.$pass) or die(pg_last_error());
return true;
}
public function query ($query)
{
$this->nQuerys++;
return pg_query($this->sqlcon, $query);
}
public function select ($table, $fields=NULL, $wFields=NULL, $wValues=NULL, $order=NULL, $limit=NULL)
{
if(is_array($fields))
{
$fields = implode(',',$fields);
}
$strQuery = 'SELECT '.$fields.' FROM '.$table;
if(is_array($wFields) && is_array($wValues) && count($wFields) == count($wValues))
{
$strQuery .= ' WHERE ';
for($i = 0; $i <= count($value)-1; $i++)
{
if($i != 0)
$strQuery .= ' AND ';
$strQuery .= $field[$i].'=\''.pg_escape_string($value[$i]).'\'';
}
}
elseif(isset($wValues) && isset($wFields))
{
$strQuery .= ' WHERE '.$wFields.'=\''.pg_escape_string($wValues).'\'';
}
if(isset($order))
{
$strQuery .= ' ORDER BY '.$order;
}
if(isset($limit) && is_numeric(str_replace(',', '', $limit)))
{
$strQuery .= ' LIMIT '.$limit;
}
return $this->query($strQuery);
}
public function insert ($table, $name, $value)
{
if(is_string($table) && is_array($name) && is_array($value) && count($name) == count($value))
{
$name = implode(',', $name);
$values = array();
foreach($value as $v)
{
$values[] = '\''.pg_escape_string($v).'\'';
}
$value = implode(',', $values);
}
return $this->query('INSERT INTO '.$table.' ('.$name.') VALUES ('.$value.')');
}
public function delete ($table, $field=NULL, $value=NULL, $limit=NULL)
{
$strQuery = 'DELETE FROM '.$table;
if(is_string($field) && isset($value))
{
$strQuery .= ' WHERE '.$field.'=\''.pg_escape_string($value).'\'';
}
elseif(is_array($field) && is_array($value) && count($field) == count($value))
{
$strQuery .= ' WHERE ';
for($i = 0; $i <= count($value)-1; $i++)
{
if($i != 0)
$strQuery .= ' AND ';
$strQuery .= $field[$i].'=\''.pg_escape_string($value[$i]).'\'';
}
}
if(isset($limit) && is_numeric($limit))
{
$strQuery .= ' LIMIT '.$limit;
}
return $this->query($strQuery);
}
public function fetchobject ($query)
{
return pg_fetch_object($query);
}
public function fetcharray ($query)
{
return pg_fetch_array($query);
}
public function disconnect ()
{
@pg_close($this->sqlcon);
}
public function __destruct ()
{
$this->disconnect();
}
}
?>
<?php
/**
* @package SQL
* @author Wim Mariën (http://www.gdx.be/)
* @copyright (c) 2007 Wim Mariën - gdx.be
* @license http://www.opensource.org/licenses/gpl-license.php
*/
class pgsql implements sql
{
protected $sqlcon ;
public $nQuerys = 0 ;
public $fetch ;
public function __construct( $host = NULL , $user = NULL , $pass = NULL , $db = NULL , $port = NULL )
{
{
$this -> connect ( $host , $user , $pass , $db , $port ) ;
}
}
public function connect ( $host , $user , $pass , $db , $port = '5432' )
{
$pass = ( ( $pass == NULL ) ? '' : ' password=' . $pass ) ;
return true ;
}
public function query ( $query )
{
$this -> nQuerys ++;
}
public function select ( $table , $fields = NULL , $wFields = NULL , $wValues = NULL , $order = NULL , $limit = NULL )
{
{
}
$strQuery = 'SELECT ' . $fields . ' FROM ' . $table ;
{
$strQuery .= ' WHERE ' ;
for ( $i = 0 ; $i <= count ( $value ) - 1 ; $i ++ ) {
if ( $i != 0 )
$strQuery .= ' AND ' ;
$strQuery .= $field [ $i ] . '=\' '.pg_escape_string($value[$i]).' \'' ;
}
}
{
$strQuery .= ' WHERE ' . $wFields . '=\' '.pg_escape_string($wValues).' \'' ;
}
{
$strQuery .= ' ORDER BY ' . $order ;
}
{
$strQuery .= ' LIMIT ' . $limit ;
}
return $this -> query ( $strQuery ) ;
}
public function insert ( $table , $name , $value )
{
{
foreach ( $value as $v )
{
$values [ ] = '\' '.pg_escape_string($v).' \'' ;
}
}
return $this -> query ( 'INSERT INTO ' . $table . ' (' . $name . ') VALUES (' . $value . ')' ) ;
}
public function delete ( $table , $field = NULL , $value = NULL , $limit = NULL )
{
$strQuery = 'DELETE FROM ' . $table ;
{
$strQuery .= ' WHERE ' . $field . '=\' '.pg_escape_string($value).' \'' ;
}
{
$strQuery .= ' WHERE ' ;
for ( $i = 0 ; $i <= count ( $value ) - 1 ; $i ++ ) {
if ( $i != 0 )
$strQuery .= ' AND ' ;
$strQuery .= $field [ $i ] . '=\' '.pg_escape_string($value[$i]).' \'' ;
}
}
{
$strQuery .= ' LIMIT ' . $limit ;
}
return $this -> query ( $strQuery ) ;
}
public function fetchobject ( $query )
{
}
public function fetcharray ( $query )
{
}
public function disconnect ( )
{
}
public function __destruct ( )
{
$this -> disconnect ( ) ;
}
}
?>
Download code (.txt)
Stemmen
Niet ingelogd.