Version: 1.0
Type: Class
Category: Databases
License: GNU General Public License
Description: A class for MySQL Database. Very similar syntax to Perl DBI. Sorry for no documentation.
<?php
/*
PHP MySQL DataBase Interface - phpdbi
v1.0 2001/07/12
Copyright(c) 2001 Chin-pang LI, Joe
*/
class DBI {
// properties
var $dbh = FALSE;
var $dbi_debug = TRUE;
var $err = 0;
var $errstr = "";
// methods
function update_state() {
$this->err = @mysql_errno();
$this->errstr = @mysql_errstr();
}
function connect($dbname, $hostname = "localhost", $username = "", $password = "") {
$bdd = FALSE;
if ($this->dbi_debug) {
$this->dbh = mysql_connect($hostname, $username, $password);
} else {
$this->dbh = @mysql_connect($hostname, $username, $password);
}
$this->update_state();
if ($this->dbh) {
if ($this->dbi_debug) {
$bdd = mysql_select_db($dbname, $this->dbh);
} else {
$bdd = @mysql_select_db($dbname, $this->dbh);
}
$this->update_state();
if ($bdd) {
return $this->dbh;
} else {
die($this->errstr);
}
} else {
die($this->errstr);
}
}
function execute($statement) {
$id = FALSE;
if ($this->dbi_debug) {
$id = mysql_query($statement);
} else {
$id = @mysql_query($statement);
}
$this->update_state();
return $id;
}
function finish($sth) {
$bdd = FALSE;
$i = 0;
$temp = array();
if ($sth > 0) {
if ($this->dbi_debug) {
$bdd = mysql_free_result($sth);
} else {
$bdd = @mysql_free_result($sth);
}
$this->update_state();
}
return ($bdd != FALSE);
}
function dosql($statement) {
$sth = $this->execute($statement);
$nrows = -1;
if ($sth) {
if ($this->dbi_debug) {
$nrows = mysql_affected_rows($sth);
} else {
$nrows = @mysql_affected_rows($sth);
}
}
$this->update_state();
$this->finish($sth);
return $nrows;
}
function fetchrow_hash($sth) {
$ret = array();
if ($sth > 0) {
if ($this->dbi_debug) {
$ret = mysql_fetch_array($sth);
} else {
$ret = @mysql_fetch_array($sth);
}
$this->update_state();
}
return $ret;
}
function fetchrow_array($sth) {
$ret = array();
if ($sth > 0) {
if ($this->dbi_debug) {
$ret = mysql_fetch_row($sth);
} else {
$ret = @mysql_fetch_row($sth);
}
$this->update_state();
}
return $ret;
}
function fetchrow_object($sth) {
$ret = array();
if ($sth > 0) {
if ($this->dbi_debug) {
$ret = mysql_fetch_object($sth);
} else {
$ret = @mysql_fetch_object($sth);
}
$this->update_state();
}
return $ret;
}
function fetchall_array($sth) {
$ret = array();
$temp = array();
if ($sth > 0) {
if ($this->dbi_debug) {
while ($temp = mysql_fetch_row($sth)) {
$ret[] = $temp;
}
} else {
while ($temp = @mysql_fetch_row($sth)) {
$ret[] = $temp;
}
}
$this->update_state();
}
return $ret;
}
function fetchall_hash($sth) {
$ret = array();
$temp = array();
if ($sth > 0) {
if ($this->dbi_debug) {
while ($temp = mysql_fetch_array($sth)) {
$ret[] = $temp;
}
} else {
while ($temp = @mysql_fetch_array($sth)) {
$ret[] = $temp;
}
}
$this->update_state();
}
return $ret;
}
function rows($sth) {
$ret = 0;
if ($sth > 0) {
if ($this->dbi_debug) {
$ret = mysql_num_rows($sth);
} else {
$ret = @mysql_num_rows($sth);
}
$this->update_state();
}
return $ret;
}
function commit($sth) {
return ($this->dosql("commit;") >= 0);
}
function rollback($sth) {
return ($this->dosql("rollback;") >= 0);
}
function quote($string) {
return @mysql_escape_string($string);
}
function err() {
return $this->err;
}
function errstr() {
return $this->errstr;
}
function disconnect() {
$bdd = FALSE;
if ($this->dbi_debug) {
$bdd = mysql_close($this->dbh);
} else {
$bdd = @mysql_close($this->dbh);
}
$this->update_state();
return ($bdd != FALSE);
}
function selectrow_array($statement) {
$ret = array();
$sth = $this->execute($statement);
$ret = $this->fetchrow_array($sth);
$this->finish($sth);
return $ret;
}
function selectrow_hash($statement) {
$ret = array();
$sth = $this->execute($statement);
$ret = $this->fetchrow_hash($sth);
$this->finish($sth);
return $ret;
}
function selectrow_object($statement) {
$ret = array();
$sth = $this->execute($statement);
$ret = $this->fetchrow_object($sth);
$this->finish($sth);
return $ret;
}
function selectall_array($statement) {
$ret = array();
$sth = $this->execute($statement);
$ret = $this->fetchall_array($sth);
$this->finish($sth);
return $ret;
}
function selectall_hash($statement) {
$ret = array();
$sth = $this->execute($statement);
$ret = $this->fetchall_hash($sth);
$this->finish($sth);
return $ret;
}
function insert_id() {
$ret = -1;
if ($this->dbh) {
if ($this->dbi_debug) {
$ret = mysql_insert_id($this->dbh);
} else {
$ret = @mysql_insert_id($this->dbh);
}
$this->update_state();
}
return $ret;
}
}
?>