Source for file EDB_MYSQL.php
Documentation is available at EDB_MYSQL.php
* Project: EDB_MYSQL :: MySQL abstraction layer
* File: EDB/EDB_MYSQL.php
* The EDB_MYSQL class is mysql 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_MYSQL class
* The number of query parameter
// {{{ (object) EDB_MYSQL::__construct ($host, $user, $pass, $db)
* Instantiates an EDB_MYSQL object and opens an mysql database
* $db = new EDB_MYSQL ('mysql://localhost', 'user', 'host', 'database');
* $db = new EDB_MYSQL ('mysql://localhost:3306', 'user', 'host', 'database');
* $db = new EDB_MYSQL ('mysql://localhost:/var/run/mysqld/mysql.sock', 'user', 'host', 'database');
* If you add prefix 'p~' before host, you can connect with persistent
* $db = new EDB_MYSQL ('mysql://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 ('MySQL extension is not loaded on PHP!', E_USER_ERROR);
if ( preg_match ('/^([^:]+):(.+)/', $o->host, $matches) ) {
$o->host = ':' . $matches[2];
$func = 'mysql_pconnect';
$this->db = $func ($o->host, $o->user, $o->pass);
mysql_select_db ($o->db, $this->db);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_MYSQL::get_charset (void)
* Get character set of current database
* MySQL extension don't support this function
* @return string Current character set name on DB
$r = $this->query ('SHOW variables WHERE Variable_name = ?', 's', 'character_set_client');
$row = $this->fetch (true);
// {{{ (bool) EDB_MYSQL::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
if ( ($r = mysql_set_charset ($char, $this->db)) == false )
$this->error = mysql_error ($this->db);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_MYSQL::escape ($string)
* Escape special characters in a string for use in an SQL statement
* @param string The string that is to be escaped.
return mysql_real_escape_string ($string, $this->db);
// {{{ (int) EDB_MYSQL::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_MYSQL::lastId (void)
* 마지막 실행한 쿼리에서 자동으로 생성된 ID(auto increment)값을 반환
return mysql_insert_id ($this->db);
// {{{ (boo) EDB_MYSQL::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 mysql_data_seek ($this->result, $offset);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (object) EDB_MYSQL::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) {
$r = mysql_fetch_object ($this->result);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (array) EDB_MYSQL::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.
while ( ($r = $this->fetch ()) !== false )
// {{{ (bool) EDB_MYSQL::free_result (void)
* Frees stored result memory for the given statement handle
if ( ! $this->free ) return true;
return mysql_free_result ($this->result);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_MYSQL::field_name ($index)
* Get the name 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/function.mysql-field-name.php mysql_field_name()
return mysql_field_name ($this->result, $index);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (string) EDB_MYSQL::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/function.mysql-field-type.php mysql_field_type()
return mysql_field_type ($this->result, $index);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (int) EDB_MYSQL::num_fields (void)
* Get number of fields in result
* @see http://php.net/manual/en/function.mysql-num-fields.php mysql_num_fields()
return mysql_num_fields ($this->result);
} catch ( Exception $e ) {
throw new myException ($e->getMessage (), $e->getCode(), $e);
// {{{ (void) EDB_MYSQL::trstart (void)
$this->db->query ('BEGIN');
// {{{ (void) EDB_MYSQL::trend ($v)
* @param bool false일경우 rollback을 수행한다.
function trend ($v = true) {
$sql = ($v === false) ? 'ROLLBACK' : 'COMMIT';
// {{{ (void) EDB_MYSQL::close (void)
// {{{ private (int) EDB_MYSQL::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 = mysql_query ($sql, $this->db);
if ( mysql_errno ($this->db) ) {
throw new myException (mysql_error ($this->db), 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 mysql_affected_rows ($this->db);
return mysql_num_rows ($this->result);
// {{{ private (int) EDB_MYSQL::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) {
if ( $this->pno != count ($params) || $this->check_param ($params) === false ) {
'Number of elements in query doesn\'t match number of bind variables',
$parano = strlen ($params[0]);
for ( $i= 0, $j= 1; $i< $parano; $i++ , $j++ ) {
switch ($params[0][$i]) {
$params[$j] = $this->escape ($params[$j]->data);
$params[$j] = $this->escape ($params[$j]);
return $this->no_bind_query ($query);
} 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
|