// $Id: dbi_mysql.php,v 1.8 2002/02/11 15:09:27 erwin Exp $
// This class is based on the Perl DBI.
// New functionality should be added accordingly.
// Please refer to the DBI documentation (perldoc DBI).
class DBI {
// Unauthorized
var $Driver_name = "mySQL";
var $Name;
var $session;
var $debug = 1;
var $err;
var $errstr="";
var $RaiseError = 1;
function DBI ($db, $user, $password) {
$this->session = mysql_pconnect('localhost', $user, $password);
mysql_select_db($db,$this->session);
$this->Name = $db;
}
function prepare ($query) {
return new STH($this, $this->session, $query, $this->debug);
}
function dbh_do($query) {
// --OBS-- returns -1 on failure, 0 is a valid return from this function!
$res = mysql_query($query, $this-> session);
if ($res) {
$this->err = 0;
$this->errstr = "";
return mysql_affected_rows($this->session);
} else {
$this->err = mysql_errno($this->session);
$this->errstr = mysql_error($this->session);
return -1;
}
}
function insert_id($sequence="") {
// $sequence only in for compatibility ie. with the postgreSQL-module
return mysql_insert_id($this->session);
}
function autocommit ($value) {
echo "
Warning: Transactions not supported by this driver.
";
return 0;
}
function commit () {
echo "
Warning: Transactions not supported by this driver.
";
return 0;
}
function rollback () {
echo "
Warning: Transactions not supported by this driver.
";
return 0;
}
function quote ($str) {
if (get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return "'".AddSlashes($str)."'";
}
}
class STH {
var $query;
var $debug;
var $session;
var $res;
var $row;
var $placeholders;
var $dbi;
function STH (&$dbi, $session, $query, $debug) {
$this->dbi = &$dbi;
$this->session = $session;
$this->query = $query;
$this->debug = $debug;
// Scan for placeholders
$this->placeholders = array();
$quote = '';
for ($i = 0; $i < strlen($query); ++$i) {
if ($query[$i] == "'") {
if (empty($quote)) {
$quote = "'";
} elseif ($quote == "'") {
$quote = '';
}
} elseif ($query[$i] == '"') {
if (empty($quote)) {
$quote = '"';
} elseif ($quote == '"') {
$quote = '';
}
} elseif ($query[$i] == '?') {
if (empty($quote)) {
array_push($this->placeholders, $i);
}
}
}
}
function execute () {
global $_SERVER;
$SERVER_NAME=$_SERVER['SERVER_NAME'];
$numargs = func_num_args();
$arg_list = func_get_args();
$parms = array();
for ($i = 0; $i < $numargs; $i++) {
if (is_array($arg_list[$i])) {
while (list($dummy,$parm) = each ($arg_list[$i])) {
array_push($parms, $parm);
}
} else {
array_push($parms,$arg_list[$i]);
}
}
if (sizeof($parms) != sizeof($this->placeholders)) {
print "
SQL Query contains ".sizeof($this->placeholders)." placeholders but ".sizeof($parms)." was passed
";
exit;
}
if (sizeof($parms) > 0) {
$query = substr($this->query, 0, $this->placeholders[0]);
for ($i = 0; $i < sizeof($parms) - 1; ++$i) {
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1, $this->placeholders[$i + 1] - $this->placeholders[$i] - 1);
}
$query .= $this->dbi->quote($parms[$i]) . substr($this->query, $this->placeholders[$i] + 1);
} else {
$query = $this->query;
}
if ($this->debug) { // Log the query
//$fd = fopen("/home/www/web1/html/new/admin/lib/dbi/tmp/dbi.$SERVER_NAME.log", "a") or die ("Couldn't append to file");
//fputs($fd, $query."\n==================\n");
//fclose($fd);
}
$this->res = @mysql_query($query, $this->session);
if (!$this->res) {
if ($this->dbi->RaiseError) {
print "
Could not execute SQL query: \" ". $query . "\"
";
print "
".mysql_errno($this->session) . " " .mysql_error($this->session)."
";
exit;
}
$this->dbi->errstr = mysql_error($this->session);
$this->dbi->err = mysql_errno($this->session);
}
$this->row = 0;
return $this->res;
}
function fetchrow_array () {
$this->row++;
return mysql_fetch_row($this->res);
}
function fetchrow_hash () {
$this->row++;
return mysql_fetch_array($this->res, MYSQL_ASSOC);
}
function finish () {
mysql_free_result($this->res);
}
function rows () {
return mysql_num_rows($this->res);
}
}
?>