<?php
/**
* @Package: FAR-PHP CMS
* @Author: Fanache A. Remus (Birkoff)
* @$Date: 2012-02-09 19:44:03 +0200 (J, 09 feb. 2012) $
* @Contact: www.farsoft.far-php.ro - contact@far-php.ro
* @$Rev: 4 $
* @$Id: mysql.inc.php 4 2012-02-09 17:44:03Z seimour_birkoff $
*
* @Desc: MySQL class for php > 5.0.x
*
* @Note:
* - for older version (< 5.x) please use mysql.inc.php
* - you can find older version on repository on http://sqlclassphp.sourceforge.net/
**/
// This file can not be accessed independently from index
if(!defined('FAR_ANTIHACK')) { header('HTTP/1.0 403 Forbidden'); header('Status: 403 Forbidden'); die("<br>You don't have access here."); }
class MYSQLI_DB {
// initializing the internal variables
// monitoring data
public $sql_tracer = array();
// if true provides information on every function called, otherwise only provides information of queries
public $sql_debug = true;
public $sql_query_monitor=true;
// connecting
public $conn = NULL;
// variable that holds the results returned
public $result = false;
// db info
protected $db_server = '127.0.0.1';
protected $db_port = '3306';
protected $db_username = 'root';
protected $db_password = '';
protected $db_name = '';
protected $db_encoding = 'utf8';
protected $db_time_zone = 'Europe/Bucharest';
protected $db_persist = false;
/**
* class instance
*
* @param str $db_server - sql server name
* @param int $db_port - sql server connection port
* @param str $db_username - sql username
* @param str $db_password - sql password
* @param str $db_name - database name
* @param str $db_encoding - encoding data type
* @param bool $db_persist - connection type
*
**/
public function __construct ($db_server='', $db_port='', $db_username='', $db_password='', $db_name='', $db_encoding='') {
if ( ! empty($db_server) )
$this->db_server = $db_server;
if ( ! empty($db_port) && is_numeric($db_port) )
$this->db_port = $db_port;
if ( ! empty($db_username) )
$this->db_username = $db_username;
if ( ! empty($db_password) )
$this->db_password = $db_password;
if ( ! empty($db_name) )
$this->db_name = $db_name;
if ( ! empty($db_encoding) )
$this->db_encoding = $db_encoding;
return $this->open();
}
/**
* connecting to sql server
* database selection
* encoding type setting
*
* @return resource $conn - false on failure
*
**/
protected function open() {
if ( $this->connect() === false )
return false;
//if ( $this->select_db() === false )
// return false;
if ( $this->set_charset() === false )
return false;
return $this->conn;
}
/**
* connecting to the database
*
* @param str $db_server
* @param int $db_port
* @param str $db_username
* @param str $db_password
*
* @return bool
*
**/
protected function connect($db_server='', $db_port='', $db_username='', $db_password='', $db_name='') {
if ( ! empty($db_server) )
$this->db_server = $db_server;
if ( ! empty($db_port) && is_numeric($db_port) )
$this->db_port = $db_port;
if ( ! empty($db_username) )
$this->db_username = $db_username;
if ( ! empty($db_password) )
$this->db_password = $db_password;
if ( ! empty($db_name))
$this->db_name = $db_name;
// if it is already connected
if( is_resource($this->conn) )
return true;
// Choose the appropriate connect function
// @Note: persistent connection is not implementted on this class,
// if you want to use persistent connection with mysqli follow instructions from this article http://www.php.net/manual/en/mysqli.persistconns.php
$this->conn = @new mysqli($this->db_server, $this->db_username, $this->db_password, $this->db_name, $this->db_port);
// Connect to the database server
if( ! is_resource($this->conn) || mysqli_connect_error() ) {
if ( $this->sql_debug )
$this->sql_monitor('connect', 'connect', mysqli_connect_error(), $this->conn, __LINE__, __FILE__, mysqli_connect_errno(), mysqli_connect_error());
return false;
} else {
if ( $this->sql_debug )
$this->sql_monitor('connect', 'connect', mysqli_connect_error(), $this->conn, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return true;
}
}
/**
* database selection
*
* @param str $db_name
*
* @return bool
*
**/
public function select_db($db_name='') {
if( empty($db_name) )
$db_name = $this->db_name;
$rez = $this->conn->select_db($db_name);
if ( $this->sql_debug )
$this->sql_monitor('select_db', 'db_name', $db_name, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $rez;
}
/**
* charset setting used
*
* @param str $db_encoding
*
* @return bool
*
**/
public function set_charset($db_encoding='') {
if ( empty($db_encoding) )
$db_encoding = $this->db_encoding;
$rez = $this->conn->set_charset($db_encoding);
if ( $this->sql_debug )
$this->sql_monitor('set_charset', 'db_encoding', $db_encoding, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $rez;
}
/**
* set time zone
*
* @param str $time_zone
*
* @return bool
*/
public function set_time_zone($time_zone='') {
if ( empty($time_zone) )
$db_time_zone = $this->db_time_zone;
else
$db_time_zone = $time_zone;
$rez = $this->query("SET time_zone = '$db_time_zone'");
if ( $this->sql_debug )
$this->sql_monitor('set_time_zone', 'time_zone', $time_zone, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $rez;
}
/**
* monitoring data and errors
*
* @param str $function_name - function name that is monitoring
* @param str $param_name - function parameter name
* @param obj $param_val - function parameter value
* @param obj $return_val - value returned
* @param int $line - line number where you can find values
* @param str $file - file name
* @param str $error - reported error
*
**/
protected function sql_monitor($function_name, $param_name, $param_val, $return_val, $line, $file, $error_nr=0, $error_val=0) {
$this->sql_tracer[] = array(
'function_name' => $function_name,
'param_name' => $param_name,
'param_val' => $param_val,
'return_val' => $return_val,
'line' => $line,
'file' => $file,
'error_nr' => $error_nr,
'error_val' => $error_val,
);
}
/**
* monitoring queryes
*
* @param str $function_name - function name that is monitoring
* @param str $param_name - function parameter name
* @param obj $param_val - function parameter value
* @param obj $return_val - value returned
* @param int $line - line number where you can find values
* @param str $file - file name
* @param str $error - reported error
*
**/
protected function query_log($function_name, $param_name, $param_val, $return_val, $line, $file, $error_nr=0, $error_val=0) {
$this->sql_query_log[] = array(
'function_name' => $function_name,
'param_name' => $param_name,
'param_val' => $param_val,
'return_val' => $return_val,
'line' => $line,
'file' => $file,
'error_nr' => $error_nr,
'error_val' => $error_val,
);
}
/**
* identification number of the current error - mysql_errno
*
* @return int $nr
*
**/
public function sql_errno() {
if ( ! is_resource($this->conn) )
return 0;
else
return $this->conn->errno;
}
/**
* last error reported - mysql_error
*
* @return str $error
*
**/
public function sql_error() {
if ( ! is_resource($this->conn) )
return 0;
else
return $this->conn->error;
}
/**
* making query - mysql_query
*
* @param str $query
* @param int $how - 0 MYSQLI_STORE_RESULT , 1 MYSQLI_USE_RESULT
* @return resource $result
*
**/
public function query($query, $how=0) {
if ( empty($query) )
return false;
if ( $how == 0)
$this->result = $this->conn->query($query);
else
$this->result = $this->conn->query($query, MYSQLI_USE_RESULT);
if ( $this->sql_debug )
$this->sql_monitor('query', 'query', $query, $this->result, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
if ( $this->sql_query_monitor )
$this->query_log('query', 'query', $query, $this->result, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $this->result;
}
/**
* parsing the data as array - mysql_fetch_array
*
* @param resource $rezultat
* @param bool $type - true for MYSQL_ASSOC, false for MYSQL_NUM, null for MYSQL_BOTH
*
* @return array $result
*
**/
public function fetch_array($rezultat=NULL, $type=true) {
if ( is_resource($rezultat) )
$result = $rezultat;
else
$result = $this->result;
if ( $type == true)
$rez = $result->fetch_array(MYSQLI_ASSOC);
elseif ( $type == false)
$rez = $result->fetch_array(MYSQLI_NUM);
else
$rez = $result->fetch_array(MYSQLI_BOTH);
if ( $this->sql_debug )
$this->sql_monitor('fetch_array', 'result', $result, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $rez;
}
/**
* parsing the data as array - mysql_fetch_array
*
* @param resource $rezultat - The result resource that is being evaluated. This result comes from a call to mysql_query().
* @param str $class_name - The name of the class to instantiate, set the properties of and return. If not specified, a stdClass object is returned
* @param array $params - An optional array of parameters to pass to the constructor for class_name objects.
*
* @return array $result
*
**/
public function fetch_object($rezultat=NULL, $class_name=NULL, $params=NULL) {
if ( is_resource($rezultat) )
$result = $rezultat;
else
$result = $this->result;
if (is_string($class_name) && ! empty($class_name)) {
if (is_array($params) && count($params)) {
$rez = $result->fetch_object($class_name, $params);
} else {
$rez = $result->fetch_object($class_name);
}
} else {
$rez = $result->fetch_object();
}
if ( $this->sql_debug )
$this->sql_monitor('fetch_object', 'result', $result, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $rez;
}
/**
* closing connection - mysql_close
*
* @return bool $rez
*
**/
public function close() {
if (is_resource($this->conn)) {
$thread_id = $this->conn->thread_id;
$this->conn->kill($thread_id);
$rez = $this->conn->close();
} else {
$rez = $this->conn;
}
if ( $this->sql_debug )
$this->sql_monitor('close', '', '', $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $rez;
}
/**
* destructor of this class - for now, only close sql connection
*
* @return bool
*/
public function __destruct() {
return $this->close();
}
/**
* start tranzaction
*
* @return resource
*
**/
public function start_tranzaction() {
return $this->conn->autocommit(FALSE);
// $this->conn->begin_transaction();
}
/**
* rollback tranzaction
*
* @return resource
*
**/
public function rollback() {
$res = $this->conn->rollback();
$this->conn->autocommit(TRUE);
return $res;
}
/**
* commit tranzaction
*
* @return resource
*
**/
public function commit() {
$res = $this->conn->commit();
$this->conn->autocommit(TRUE);
return $res;
}
/**
* number of rows affected - mysql_affected_rows
*
* @return int $rows
*
**/
public function affected_rows() {
$rows = $this->conn->affected_rows;
if ( $this->sql_debug )
$this->sql_monitor('affected_rows', '', '', $rows, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $rows;
}
/**
* inserted row id - mysql_insert_id
*
* @return int $id
*
**/
public function insert_id() {
$id = $this->conn->insert_id;
if ( $this->sql_debug )
$this->sql_monitor('insert_id', '', '', $id, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $id;
}
/**
* filter values - mysql_real_escape_string
*
* @param mixed $str
*
* @return mixed $str
*
**/
public function real_escape($str) {
if ( is_array($str) ) {
$r = array();
foreach($str as $k=>$v) {
$r[$k] = $this->conn->real_escape_string($v);
}
$rez = $r;
} else {
$rez = $this->conn->real_escape_string($str);
}
if ( $this->sql_debug )
$this->sql_monitor('real_escape', 'str', $str, $rez, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $rez;
}
/**
* number of rows found - mysql_num_rows
*
* @param resource $rezultat
*
* @return int $result
*
**/
public function num_rows($rezultat=NULL) {
if ( ! is_resource($rezultat) )
$rez = $this->result;
else
$rez = $rezultat;
$result = $rez->num_rows;
if ( $this->sql_debug )
$this->sql_monitor('num_rows', 'rez', $rez, $result, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $result;
}
/**
* result returned by the query - mysql_result
*
* @param resource $rezultat
* @param int $row
*
* @return str $result
*
**/
public function result($rezultat=NULL, $row=0, $field=0) {
if ( ! is_resource($rezultat) )
$rez = $this->result;
else
$rez = $rezultat;
// @TODO de testat
$rez->data_seek($row);
$datarow = $rez->fetch_array();
$result = $datarow[$field];
if ( $this->sql_debug )
$this->sql_monitor('result', 'rez', $rez, $result, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $result;
}
/**
* information about SQL - mysql_info
*
* @return str $info
*
**/
public function sql_info() {
$info = $this->conn->info;
if ( $this->sql_debug )
$this->sql_monitor('sql_info', '', '', $info, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $info;
}
/**
* buffer empty sql - mysql_free_result
*
* @param resource $rez
*
* @return bool
*
**/
public function free_result($rezultat=NULL) {
if ( ! is_resource($rezultat) )
$rez = $this->result;
else
$rez = $rezultat;
if (is_object($rez))
$info = $rez->free();
else
$info = false;
//$info = $rez->close();
if ( $this->sql_debug )
$this->sql_monitor('free_result', '', '', $info, __LINE__, __FILE__, $this->sql_errno(), $this->sql_error());
return $info;
}
/**
* Preparation for working with MySQL data
*
* @param array $values - values that need to be changed
* @param bool $tilde - what type of quotes used
*
* @return string $values
*
**/
public static function prepare_data($values, $tilde = false) {
if (! is_array($values) || count($values) == 0)
return false;
if ($tilde)
$values = count($values) ? "`" . implode("`, `", $values) . "`" : '';
else
$values = count($values) ? "'" . implode("', '", $values) . "'" : '';
return $values;
}
/**
* Filter an array by keys
*
* @param array $array associative array to filter
* @param array $filter list of keys to return (in this order)
*
* @return array a new array containing only those keys and values from $array which exists in $filter too (keep order from $filter)
*
**/
public static function array_sqlfilter($array, $filter) {
if (! is_array($array) || count($array) == 0)
return false;
if (! is_array($filter) || count($filter) == 0)
return false;
$result = array();
foreach ($filter as $key) {
if (isset($array[$key]))
$result[$key] = $array[$key];
}
return $result;
}
/**
* return server version
*
*/
public function get_server_info() {
return $this->conn->server_info;
}
/**
* return client sql version
*/
public function get_client_info() {
return mysqli_get_client_info();
}
}
?>
|