<?php
/*
* MySQLi wrapper class
*/
class e_db
{
public $prefix;
private $db, $dblink, $query_count = 0;
function __construct( $arr )
{
$this->db = new mySQLi();
$this->db->init();
$this->db->real_connect( $arr['host'], $arr['user'], $arr['password'], $arr['db'], $arr['port'], $arr['sock'] );
$this->db->set_charset('utf8');
$this->prefix = $arr['prefix'];
}
function __destruct()
{
e_debug::log( "Page query count = " . $this->query_count );
$this->db->close();
}
/*
* Database query
*/
function res( $str, $debug = 0 )
{
if ( $debug )
{
e_debug::log( $str );
return;
}
$this->query_count++;
$ret = $this->db->real_query( $str );
if ( $ret )
return $ret;
else
{
e_debug::log( $this->db->error );
return false;
}
}
/*
* Fetch database row
*/
function row( $str, $debug = 0 )
{
if ( !$this->res( $str, $debug ) )
return false;
$result = $this->db->use_result();
if ( !$result )
return false;
return $result->fetch_array();
}
/*
* Fetch a column value from a database row
*/
function value( $str, $debug = 0 )
{
if ( !( $row = $this->row( $str, $debug ) ) )
return false;
return $row[0];
}
/*
* Fetch an array of single column values from database table rows
*/
function values( $str, $debug = 0 )
{
$arr = array();
if ( !$this->res( $str, $debug ) )
return false;
$result = $this->db->use_result();
if ( !$result )
return false;
while( $row = $result->fetch_row() )
$arr[] = $row[0];
return $arr;
}
/*
* Fetch array of rows
*/
function arr( $str, $debug = 0 )
{
$arr = array();
if ( !$this->res( $str, $debug ) )
return false;
$result = $this->db->use_result();
if ( !$result )
return false;
while( $row = $result->fetch_array() )
$arr[] = $row;
return $arr;
}
function last_id()
{
return mysqli_insert_id( $this->db );
}
};
?>