Source for file EDB_MYSQLI.php
Documentation is available at EDB_MYSQLI.php
* Project: EDB_MYSQLI :: MySQLi abstraction layer
* File: EDB/EDB_MYSQLI.php
* The EDB_MYSQLI class is mysqli abstraction layer that used internally
* @subpackage EDB_ABSTRACT
* @author JoungKyun.Kim <http://oops.org>
* @copyright (c) 2018, JoungKyun.Kim
* @link http://pear.oops.org/package/EDB
* MySQLi engine for EDB API
* This class support abstracttion DB layer for MySQLi Engine
* db handler of EDB_MYSQLI class
* The number of query parameter
* The number of query parameter
private $field = array ();
// {{{ (object) EDB_MYSQLI::__construct ($host, $user, $pass, $db)
* Instantiates an EDB_MYSQLI object and opens an mysql database
* $db = new EDB_MYSQLI ('mysqli://localhost', 'user', 'host', 'database');
* $db = new EDB_MYSQLI ('mysqli://localhost:3306', 'user', 'host', 'database');
* $db = new EDB_MYSQLI ('mysqli://localhost:/var/run/mysqld/mysql.socl', 'user', 'host', 'database');
* If you add prefix 'p~' before host, you can connect with persistent
* $db = new EDB_MYSQLI ('mysqli://p~localhost', 'user', 'host', 'database');
* @param string $hostname mysql host
* @param string $user mysql user
* @param string $password mysql password
* @param string $database mysql database
$argv = is_array ($_argv[0]) ? $_argv[0] : $_argv;;
throw new myException ('MySQLi extension is not loaded on PHP!', E_USER_ERROR);
if ( preg_match ('/([^:]+):(.*)/', $o->host, $matches) ) {
// set persistent connect
$this->db = new mysqli ($o->host, $o->user, $o->pass, $o->db, $o->port, $o->sock);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_MYSQLI::get_charset (void)
* Get character set of current database
* @return string Current character set name on DB
return $this->db->character_set_name ();
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (bool) EDB_MYSQLI::set_charset ($charset)
* Set character set of current database
* @return bool The name of character set that is supported on database
* @param string name of character set that supported from database
return $this->db->set_charset ($char);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_MYSQLI::escape ($string)
* Escape special characters in a string for use in an SQL statement
* @param string The string that is to be escaped.
return $this->db->real_escape_string ($string);
// {{{ (int) EDB_MYSQLI::query ($query, $param_type, $param1, $param2 ...)
* Performs a query on the database
* @return integer The number of affected rows or false
* @param string $query The query strings
* @param string $type (optional) Bind parameter type. See also
* {@link http://php.net/manual/en/mysqli-stmt.bind-param.php mysqli_stmt::bind_param}.
* @param mixed $param1 (optional) Bind parameter 1
* @param mixed $param2,... (optional) Bind parameter 2 ..
$argv = is_array ($_argv[0]) ? $_argv[0] : $_argv;;
// store query in log variable
return $this->no_bind_query ($sql);
return $this->bind_query ($sql, $argv);
// {{{ (int) EDB_MYSQLI::lastId (void)
* 마지막 실행한 쿼리에서 자동으로 생성된 ID(auto increment)값을 반환
return $this->db->insert_id;
// {{{ (bool) EDB_MYSQLI::seek ($offset)
* Adjusts the result pointer to an arbitrary row in the result
* @param integer Must be between zero and the total number of rows minus one
function seek ($offset) {
return $this->result->data_seek ($offset);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (object) EDB_MYSQLI::fetch (void)
* Fetch a result row as an associative object
* @return object The object of fetched a result row or false
* @param boolean (optional) fetch 수행 후 result를 free한다.
* (기본값: false) EDB >= 2.0.3
function fetch ($free = false) {
if ( $this->result instanceof mysqli_result )
$r = $this->fetch_result ();
else if ( $this->result instanceof mysqli_stmt )
$r = $this->fetch_stmt ();
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (array) EDB_MYSQLI::fetch_all ($free = true)
* Fetch all result rows as an associative object
* @return array The fetched result rows
* @param boolean (optional) free result set after fetch.
if ( $this->result instanceof mysqli_result ) {
return $this->fetch_result_all ($free);
} else if ( $this->result instanceof mysqli_stmt ) {
return $this->fetch_stmt_all ($free);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (bool) EDB_MYSQLI::free_result (void)
* Frees stored result memory for the given statement handle
if ( ! $this->free ) return true;
$this->result->free_result ();
if ( $this->result instanceof mysqli_stmt )
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_MYSQLI::field_name ($index)
* Get the name of the specified field in a result
* @param integer The field number. This value must be in the
* range from 0 to number of fields - 1.
$r = $this->field_info ($index, 'type');
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_MYSQLI::field_type ($index)
* Get the type of the specified field in a result
* @param integer The numerical field offset. The field_offset starts
* at 0. If field_offset does not exist, return false
* and an error of level E_WARNING is also issued.
* @see http://php.net/manual/en/mysqli.constants.php Predefined Constants
$r = $this->field_info ($index, 'type');
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
return $this->file_type_string ($r);
// {{{ (int) EDB_MYSQLI::num_fields (void)
* Returns the number of columns for the most recent query
* @return integer An integer representing the number of fields in a result set.
* @see http://php.net/manual/en/mysqli.field-count.php mysqli::$field_count
return $this->db->field_count;
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (void) EDB_MYSQLI::trstart (void)
//$this->db->query ('BEGIN');
$this->db->query ('START TRANSACTION WITH CONSISTENT SNAPSHOT');
// {{{ (void) EDB_MYSQLI::trend ($v)
* @param bool false일경우 rollback을 수행한다.
function trend ($v = true) {
$sql = ($v === false) ? 'ROLLBACK' : 'COMMIT';
// {{{ (void) EDB_MYSQLI::close (void)
// {{{ private (int) EDB_MYSQLI::no_bind_query ($sql)
* Performs a query on the database
* @return integer The number of affected rows or false
* @param string The query strings
private function no_bind_query ($sql) {
$this->result = $this->db->query ($sql);
if ( $this->db->errno ) {
throw new myException ($this->db->error, E_USER_WARNING);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
if ( preg_match ('/^(update|insert|delete|replace)/i', trim ($sql)) ) {
/* Insert or update, or delete query */
return $this->db->affected_rows;
return $this->result->num_rows;
// {{{ private (int) EDB_MYSQLI::bind_query ($sql, $parameters)
* Performs a bind query on the database
* @return integer The number of affected rows or false
* @param string The query strings
* @param array (optional) Bind parameter type
private function bind_query ($sql, $params) {
$this->result = $this->db->prepare ($sql);
throw new myException ($this->db->error, E_USER_WARNING);
if ( $this->pno != count ($params) || $this->check_param ($params) === false ) {
'Number of elements in query doesn\'t match number of bind variables',
for ( $i= 0; $i< count ($params); $i++ ) {
$param[$i] = &$params[$i];
switch ($params[0][$i- 1]) {
// don't support clob type mysqli_bind_params
$blobs[$i- 1] = is_object ($params[$i]) ? $params[$i]->data : $params[$i];
foreach ( $blobs as $key => $val ) {
if ( $this->result->send_long_data ($key, $val) === false )
if ( $this->result->execute () === false )
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
$this->bind_result ($sql);
return $this->result->affected_rows;
// {{{ private (void) EDB_MYSQLI::bind_result (void)
* Binds variables to a prepared statement for result storage
* @param string Query string
private function bind_result ($sql) {
$this->result->store_result ();
$meta = $this->result->result_metadata ();
while ( $fields = $meta->fetch_field () )
$var[] = &$this->field[$fields->name];
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ private (object) EDB_MYSQLI::fetch_result (void)
* Fetch a result row as an associative object
* @return object The object of fetched a result row or false
private function fetch_result () {
if ( $this->result instanceof mysqli_result )
$r = $this->result->fetch_object ();
// {{{ private (object) EDB_MYSQLI::fetch_stmt (void)
* Fetch a result row as an associative object
* @return object The object of fetched a result row or false
private function fetch_stmt () {
if ( ! $this->result instanceof mysqli_stmt )
if ( $fetch_check = $this->result->fetch () ) {
foreach ( $this->field as $key => $val )
// {{{ private (array) EDB_MYSQLI::fetch_result_all ($free = true)
* Fetch all result rows as an associative object
* @return array The fetched result rows
* @param boolean (optional) free result set after fetch.
private function fetch_result_all ($free = true) {
if ( ! $this->result instanceof mysqli_result )
//$this->field = array ();
while ( ($row = $this->result->fetch_object ()) !== null )
// {{{ private (array) EDB_MYSQLI::fetch_stmt_all ($free = true)
* Fetch all result rows as an associative object
* @return array The fetched result rows
* @param boolean (optional) free result set after fetch.
private function fetch_stmt_all ($free = true) {
if ( ! $this->result instanceof mysqli_stmt )
while ( $this->result->fetch () ) {
foreach ( $this->field as $key => $val )
// {{{ private (mixed) EDB_MYSQLI::field_info ($index)
* Get the type of the specified field in a result
* @param integer The numerical field offset. The field_offset starts
* at 0. If field_offset does not exist, return false
* and an error of level E_WARNING is also issued.
private function field_info ($index, $type = 'name') {
if ( $this->result instanceof mysqli_result ) {
if ( ($o = $this->result->fetch_field_direct ($index)) === false )
} else if ( $this->result instanceof mysqli_stmt ) {
if ( ($result = $this->result->result_metadata ()) === false )
for ( $i= 0; $i<= $index; $i++ )
$o = $result->fetch_field ();
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ private (string) file_type_string ($type) {
* change mysqli filed type to strings
* @param integer mysqli field type
private function file_type_string ($type) {
case MYSQLI_TYPE_DECIMAL :
//Precision math DECIMAL or NUMERIC field (MySQL 5.0.3 and up)
case MYSQLI_TYPE_NEWDECIMAL :
return 'BIT (MySQL 5.0.3 and up)';
case MYSQLI_TYPE_DOUBLE :
case MYSQLI_TYPE_TIMESTAMP :
case MYSQLI_TYPE_LONGLONG :
case MYSQLI_TYPE_DATETIME :
case MYSQLI_TYPE_NEWDATE :
case MYSQLI_TYPE_INTERVAL :
case MYSQLI_TYPE_TINY_BLOB :
case MYSQLI_TYPE_MEDIUM_BLOB :
case MYSQLI_TYPE_LONG_BLOB :
case MYSQLI_TYPE_VAR_STRING :
case MYSQLI_TYPE_STRING :
case MYSQLI_TYPE_GEOMETRY :
} catch ( Exception $e ) { }
* vim: set filetype=php noet sw=4 ts=4 fdm=marker:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
|