Old stuff/old_sites/epik/class.mysql.php
(Deskargatu)
<?php
class connection
{
var $con_id;
var $error;
var $errno;
function connection($db_user, $db_password , $db_host, $db_name)
{
$this->error = '';
$this->con_id = @mysql_connect($db_host, $db_user, $db_password);
if (!$this->con_id) {
$this->setError();
} else {
$this->database($db_name);
}
}
function database($db_name)
{
$db = @mysql_select_db($db_name);
if(!$db) {
$this->setError();
return false;
} else {
return true;
}
}
function close()
{
if ($this->con_id) {
mysql_close($this->con_id);
return true;
} else {
return false;
}
}
function selectQuery($query)
{
if (!$this->con_id) {
return false;
}
$cur = mysql_unbuffered_query($query, $this->con_id);
if ($cur)
{
$res = mysql_fetch_row($cur);
if (count($res) === 1)
{
$result = $res[0];
}
else
{
$result = array();
for($j=0; $j<count($res); $j++)
{
$result[strtolower(mysql_field_name($cur, $j))] = $res[$j];
}
}
return $result;
}
else
{
$this->setError();
return false;
}
}
function assocQuery($query)
{
if (!$this->con_id) {
return false;
}
$cur = mysql_unbuffered_query($query, $this->con_id);
if ($cur)
{
$i = 0;
$result = array();
while($res = mysql_fetch_row($cur))
{
for($j=0; $j<count($res); $j++)
{
$result[$i][strtolower(mysql_field_name($cur, $j))] = $res[$j];
}
$i++;
}
return $result;
}
else
{
$this->setError();
return false;
}
}
function execute($query)
{
if (!$this->con_id) {
return false;
}
$cur = mysql_query($query, $this->con_id);
if (!$cur) {
$this->setError();
return false;
} else {
return true;
}
}
function getLastID()
{
if ($this->con_id) {
return mysql_insert_id($this->con_id);
} else {
return false;
}
}
function setError()
{
if ($this->con_id) {
$this->error = mysql_error($this->con_id);
$this->errno = mysql_errno($this->con_id);
} else {
$this->error = mysql_error();
$this->errno = mysql_errno();
}
}
function error()
{
if ($this->error != '') {
return $this->errno.' - '.$this->error;
} else {
return false;
}
}
function escapeStr($str)
{
return mysql_escape_string($str);
}
}
?>