142 lines
3.3 KiB
Plaintext
142 lines
3.3 KiB
Plaintext
<?php
|
|
define('SQL_ASSOC', MYSQL_ASSOC);
|
|
define('SQL_NUM', MYSQL_NUM);
|
|
define('SQL_BOTH', MYSQL_BOTH);
|
|
define('CRLF', "\r\n");
|
|
class dbconnect
|
|
{
|
|
var $showError;
|
|
var $dbc;
|
|
var $bConnect; // add invalidate
|
|
var $bQueryrun; // add invalidate
|
|
|
|
function dbconnect($host, $user, $pass, $base, $file = '', $line = 0)
|
|
{
|
|
if($pass != ""){
|
|
if(!$this->dbc = mysql_connect($host, $user, $pass))
|
|
{
|
|
$error = 'Cannot connect to MySQL server. In file '.$file.' in line '.$line.'. Date: '.date('Y-m-d H:i:s').'<br />'.CRLF;
|
|
$this->bConnect= false;
|
|
//die($error);
|
|
return false;
|
|
}
|
|
}else{
|
|
if(!$this->dbc = mysql_connect($host, $user))
|
|
{
|
|
$error = 'Cannot connect to MySQL server. In file '.$file.' in line '.$line.'. Date: '.date('Y-m-d H:i:s').'<br />'.CRLF;
|
|
//die($error);
|
|
//FIXME: return? die?
|
|
$this->bConnect= false;
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if(!@mysql_query("USE ".$base, $this->dbc))
|
|
{
|
|
$error = 'Error <b>'.mysql_error($this->dbc).'</b> in query <b>'.$query.'</b>. In file '.$file.' in line '.$line.'. Date: '.date('Y-m-d H:i:s').'<br />'.CRLF;
|
|
$this->bConnect= false;
|
|
//die($error);
|
|
}else
|
|
$this->bConnect= true;
|
|
|
|
$this->showError = true;
|
|
|
|
return $this->bConnect;
|
|
}
|
|
|
|
function query($query, $file = '', $line = 0)
|
|
{
|
|
if(!$result = @mysql_query($query, $this->dbc))
|
|
{
|
|
$error = 'Error <b>'.mysql_error($this->dbc).'</b> in query <b>'.$query.'</b>. In file '.$file.' in line '.$line.'. Date: '.date('Y-m-d H:i:s').'<br />'.CRLF;
|
|
if($this->showError)
|
|
{
|
|
echo $error;
|
|
$this->bQueryrun = false;
|
|
return new dbcResult($result);
|
|
}
|
|
}
|
|
else
|
|
$this->bQueryrun = true;
|
|
|
|
return new dbcResult($result);
|
|
}
|
|
|
|
|
|
function query_bool($query, $file = '', $line = 0)
|
|
{
|
|
if(!$result = @mysql_query($query, $this->dbc))
|
|
{
|
|
$error = 'Error <b>'.mysql_error($this->dbc).'</b> in query <b>'.$query.'</b>. In file '.$file.' in line '.$line.'. Date: '.date('Y-m-d H:i:s').'<br />'.CRLF;
|
|
if($this->showError)
|
|
{
|
|
echo $error;
|
|
}
|
|
}
|
|
return ($result?true:false);
|
|
}
|
|
|
|
function insert_id()
|
|
{
|
|
$id = $this->firstCell("SELECT LAST_INSERT_ID() as id");
|
|
return $id;
|
|
}
|
|
|
|
function fetch_array($query, $type = SQL_BOTH)
|
|
{
|
|
$result = $this->query($query);
|
|
return $result->fetch_array($type);
|
|
}
|
|
|
|
function first_cell($query)
|
|
{
|
|
$result = $this->query($query);
|
|
$row = $result->fetch_array(SQL_NUM);
|
|
return $row[0];
|
|
}
|
|
|
|
function change_base($base, $file = '', $line = 0)
|
|
{
|
|
if(!$result = @mysql_query('USE '.$base, $this->dbc))
|
|
{
|
|
$error = 'Error <b>'.mysql_error($this->dbc).'</b> in query <b>'.$query.'</b>. In file '.$file.' in line '.$line.'. Date: '.date('Y-m-d H:i:s').'<br />'.CRLF;
|
|
if($this->showError)
|
|
{
|
|
echo $error;
|
|
}
|
|
}
|
|
return ($result?true:false);
|
|
}
|
|
|
|
function close()
|
|
{
|
|
@mysql_close($this->dbc);
|
|
unset($this);
|
|
}
|
|
}
|
|
|
|
class dbcResult
|
|
{
|
|
function dbcResult($result)
|
|
{
|
|
$this->result = $result;
|
|
}
|
|
|
|
function fetch_array($type = SQL_BOTH)
|
|
{
|
|
return @mysql_fetch_array($this->result, $type);
|
|
}
|
|
|
|
function first_cell()
|
|
{
|
|
$row = @mysql_fetch_row($this->result);
|
|
return $row[0];
|
|
}
|
|
|
|
function num_rows()
|
|
{
|
|
return @mysql_num_rows($this->result);
|
|
}
|
|
}
|
|
?>
|