<?php
/*
Author:Adam Więckwski
Country:Poland
E-mail:adam-wieckowski@tlen.pl
Class:SqlClass
Version:0.96
changes 0.9b->0.95:
Added error handling(except mssql and sybase)
I've found some bugs(I've tested it at least)
changes 0.95->0.96:
Added __sleep() and __wakeup() functions that are helpful when you're serialising the class. With them came some new vars (host, user etc.), but be careful, becouse the pass is only base_64 encoded. After unserializing the connection is avaible without any inits.
You can redistribute and change it as you only will
There are two classes:
sql- you should only this one, the seccond one will come to you with sql_class->exec();
___return___-I've wrote about this here upper
All methods except row_num with interbase are allowed to all database's
*/
#you can add the commentation if your including the file
if(__FILE__==str_replace('/', '\\', $_SERVER['PATH_TRANSLATED'])){
die('YOU CANNOT ACCESS THIS FILE DIRECTLY');
}
elseif(__FILE__==$_SERVER['PATH_TRANSLATED']){
die('YOU CANNOT ACCESS THIS FILE DIRECTLY');
};
class sql{
var $dbtype;
var $connection;
var $host;
var $user;
var $pass;
var $dbname;
var $conntype;
function sql($dbtype){
$this->dbtype=$dbtype;
return true;
}
#checks if you have the database support in your php
function ___makecheck___(){
switch ($this->dbtype):
case 'mysql':
if(!function_exists('mysql_connect')){
return false;
};
break;
case 'pgsql':
if(!function_exists('pg_connect')){
return false;
};
break;
case 'ODBC':
if(!function_exists('odbc_connect')){
return false;
};
break;
case 'interbase':
if(!function_exists('ibase_connect')){
return false;
};
break;
case 'msql':
if(!function_exists('msql_connect')){
return false;
};
break;
case 'mssql':
if(function_exists('mssql_connect')){
return false;
};
break;
case 'sybase':
if(!function_exists('sybase_connect')){
return false;
};
break;
endswitch;
return true;
}
#connects the database-server and the database
function connect($host, $user, $pass, $dbname){
$this->conntype=0;
switch ($this->dbtype):
case 'mysql':
if(!$this->connection=@mysql_connect($host, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
if(!@mysql_select_db($dbname, $this->connection)){
return false;
};
$this->dbname=$dbname;
return true;
break;
case 'pgsql':
if(!$this->connection=@pg_connect('host='.$host.' user='.$user.' password='.$pass.' dbname='.$dbname)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
$this->dbname=$dbname;
return true;
break;
case 'ODBC':
if(!$this->connection=@odbc_connect($host.':'.$dbname, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
$this->dbname=$dbname;
return true;
break;
case 'interbase':
if(!$this->connection=@ibase_connect($host.':'.$dbname, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
$this->dbname=$dbname;
return true;
break;
case 'msql':
if(!$this->connection=@msql_connect($host, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
if(!@msql_select_db($dbname, $this->connection)){
return false;
};
$this->dbname=$dbname;
return true;
break;
case 'mssql':
if(!$this->connection=@mssql_connect($host, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
if(!@mssql_select_db($dbname, $this->connection)){
return false;
};
$this->dbname=$dbname;
return true;
break;
case 'sybase':
if(!$this->connection=@sybase_connect($host, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
if(!@sybase_select_db($dbname, $this->connection)){
return false;
};
$this->dbname=$dbname;
return true;
break;
endswitch;
}
#connects the database-server and the database
function pconnect($host, $user, $pass, $dbname){
$this->conntype=1;
switch ($this->dbtype):
case 'mysql':
if(!$this->connection=@mysql_pconnect($host, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
if(!@mysql_select_db($dbname, $this->connection)){
return false;
};
$this->dbname=$dbname;
return true;
break;
case 'pgsql':
if(!$this->connection=@pg_pconnect('host='.$host.' user='.$user.' password='.$pass.' dbname='.$dbname)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
$this->dbname=$dbname;
return true;
break;
case 'ODBC':
if(!$this->connection=@odbc_pconnect($host.':'.$dbname, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
$this->dbname=$dbname;
return true;
break;
case 'interbase':
if(!$this->connection=@ibase_pconnect($host.':'.$dbname, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
$this->dbname=$dbname;
return true;
break;
case 'msql':
if(!$this->connection=@msql_pconnect($host, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
if(!@msql_select_db($dbname, $this->connection)){
return false;
};
$this->dbname=$dbname;
return true;
break;
case 'mssql':
if(!$this->connection=@mssql_pconnect($host, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
if(!@mssql_select_db($dbname, $this->connection)){
return false;
};
$this->dbname=$dbname;
return true;
break;
case 'sybase':
if(!$this->connection=@sybase_pconnect($host, $user, $pass)){
return false;
};
$this->host=$host;
$this->user=$user;
$this->pass=base64_encode($pass);
if(!@sybase_select_db($dbname, $this->connection)){
return false;
};
$this->dbname=$dbname;
return true;
break;
endswitch;
}
#execute the query
function exec($query){
switch ($this->dbtype):
case 'mysql':
if(!$result=@mysql_query($query, $this->connection)){
$this->error=$this->error();
return false;
};
break;
case 'pgsql':
if(!$result=@pg_query($this->connection, $query)){
$this->error=$this->error();
return false;
};
break;
case 'ODBC':
if(!$result=@odbc_exec($this->connection, $query)){
$this->error=$this->error();
return false;
};
break;
case 'interbase':
if(!$result=@ibase_query($this->connection, $query)){
$this->error=$this->error();
return false;
};
break;
case 'msql':
if(!$result=@msql_query($query, $this->connection)){
$this->error=$this->error();
return false;
};
break;
case 'mssql':
if(!$result=@mssql_query($query, $this->connection)){
$this->error=$this->error();
return false;
};
break;
case 'sybase':
if(!$result=@sybase_query($query, $this->connection)){
$this->error=$this->error();
return false;
};
break;
endswitch;
$return=new ___result___($result, $this->dbtype);
return $return;
}
#disconnects the db-server
function disconnect(){
switch ($this->dbtype):
case 'mysql':
@mysql_close($this->connection);
break;
case 'pgsql':
@pg_close($this->connection);
break;
case 'ODBC':
@odbc_close($this->connection);
break;
case 'interbase':
@ibase_close($this->connection);
break;
case 'msql':
@msql_close($this->connection);
break;
case 'mssql':
@mssql_close($this->connection);
break;
case 'sybase':
@sybase_close($this->connection);
break;
endswitch;
}
function error(){
switch ($this->dbtype):
case 'mysql':
return @mysql_errno($this->connection).':<br />'."\n".@mysql_error($this->connection);
break;
case 'pgsql':
return pg_last_error($this->connection);
break;
case 'ODBC':
return @odbc_error($this->connection).':<br />'."\n".@odbc_errormsg($this->connection);
break;
case 'interbase':
return @ibase_errmsg($this->connection);
break;
case 'msql':
return @msql_error($this->connection);
break;
case 'mssql':
return 'MSSQL doesn\'t handle errors';
break;
case 'sybase':
return 'Sybase doesn\'t handle errors';
break;
endswitch;
}
function __sleep(){
$this->disconnect();
return array('dbtype', 'host', 'user', 'pass', 'dbname');
}
function __wakeup(){
$this->conntype?$this->pconnect($this->host, $this->user, base64_decode($this->pass), $this->dbname):$this->connect($this->host, $this->user, base64_decode($this->pass), $this->dbname);
}
}
class ___result___{
var $dbtype;
var $result;
var $rownum;
function ___result___($result, $dbtype){
$this->dbtype=$dbtype;
$this->result=$result;
if(($dbtype=='pgsql') || $dbtype=='ODBC'){
$this->rownum=0;
};
}
function fetch_array(){
switch ($this->dbtype):
case 'mysql':
return mysql_fetch_array($this->result, MYSQL_ASSOC);
break;
case 'pqsql':
$return=pg_fetch_array($this->result, $this->rownum, PGSQL_ASSOC);
$this->rownum++;
return $return;
break;
case 'ODBC':
$return=odbc_fetch_array($this->result, $this->rownum);
$this->rownum++;
return $return;
break;
case 'interbase':
return get_object_vars(ibase_fetch_object($this->result));
break;
case 'msql':
return msql_fetch_array($this->result, MSQL_ASSOC);
break;
case 'mssql':
return mssql_fetch_array($this->result);
break;
case 'sybase':
return sybase_fetch_array($this->result);
break;
endswitch;
return false;
}
function fetch_object(){
switch ($this->dbtype):
case 'mysql':
return mysql_fetch_object($this->result);
break;
case 'pqsql':
$return=pg_fetch_object($this->result, $this->rownum);
$this->rownum++;
return $return;
break;
case 'ODBC':
$return=odbc_fetch_object($this->result, $this->rownum);
$this->rownum++;
return $return;
break;
case 'interbase':
return ibase_fetch_object($this->result);
break;
case 'msql':
return msql_fetch_object($this->result);
break;
case 'mssql':
return mssql_fetch_object($this->result);
break;
case 'sybase':
return sybase_fetch_object($this->result);
break;
endswitch;
return false;
}
function fetch_row(){
switch ($this->dbtype):
case 'mysql':
return mysql_fetch_row($this->result);
break;
case 'pqsql':
$return=pg_fetch_row($this->result, $this->rownum);
$this->rownum++;
return $return;
break;
case 'ODBC':
$return=odbc_fetch_row($this->result, $this->rownum);
$this->rownum++;
return $return;
break;
case 'interbase':
return ibase_fetch_row($this->result);
break;
case 'msql':
return msql_fetch_row($this->result);
break;
case 'mssql':
return mssql_fetch_row($this->result);
break;
case 'sybase':
return sybase_fetch_row($this->result);
break;
endswitch;
return false;
}
function free_result(){
switch ($this->dbtype):
case 'mysql':
mysql_free_result($this->result);
break;
case 'pgsql':
pg_free_result($this->result);
break;
case 'ODBC':
odbc_free_result($this->result);
break;
case 'msql':
ibase_free_result($this->result);
break;
case 'msql':
msql_free_result($this->result);
break;
case 'mssql':
mssql_free_result($this->result);
break;
case 'sybase':
sybase_free_result($this->result);
break;
endswitch;
return true;
}
function num_rows(){
switch ($this->dbtype):
case 'mysql':
$return=mysql_num_rows($this->result);
break;
case 'pgsql':
$return=pg_num_rows($this->result);
break;
case 'ODBC':
$return=odbc_num_rows($this->result);
break;
case 'interbase':
$return=false;
break;
case 'msql':
$return=msql_num_rows($this->result);
break;
case 'mssql':
$return=mssql_num_rows($this->result);
break;
case 'sybase':
$return=sybase_num_rows($this->result);
break;
endswitch;
return $return;
}
}
?>
|