Constants

DBOF_DEBUG_OFF

DBOF_DEBUG_OFF

DEBUG: No Debug Info

DBOF_DEBUG_SCREEN

DBOF_DEBUG_SCREEN

DEBUG: Debug to screen

DBOF_DEBUG_FILE

DBOF_DEBUG_FILE

DEBUG: Debug to error.log

DBOF_SHOW_NO_ERRORS

DBOF_SHOW_NO_ERRORS

Displays errors with reduced informations and terminates execution.

DBOF_SHOW_ALL_ERRORS

DBOF_SHOW_ALL_ERRORS

Displays errors with all available informations and terminates execution.

DBOF_RETURN_ALL_ERRORS

DBOF_RETURN_ALL_ERRORS

Returns the error code back to the callee. You are responsible for error management.

DBOF_MYSQL_COLNAME

DBOF_MYSQL_COLNAME

DescTable(): Column name.

DBOF_MYSQL_COLTYPE

DBOF_MYSQL_COLTYPE

DescTable(): Column type.

DBOF_MYSQL_COLSIZE

DBOF_MYSQL_COLSIZE

DescTable(): Column size.

DBOF_MYSQL_COLFLAGS

DBOF_MYSQL_COLFLAGS

DescTable(): Column flags.

DBOF_TYPE_INT

DBOF_TYPE_INT

Type definitions for QueryHash() / QueryResultHash()

DBOF_TYPE_DOUBLE

DBOF_TYPE_DOUBLE

DBOF_TYPE_STRING

DBOF_TYPE_STRING

DBOF_TYPE_BLOB

DBOF_TYPE_BLOB

Properties

$sock

$sock : 

Internal connection handle.

Type

$host

$host : 

Name of database to connect to

Type

$user

$user : 

Name of database user used for connection

Type

$password

$password : 

Password of database user used for connection

Type

$database

$database : 

Name of schema to be used

Type

$port

$port : 

Portnumber to use when connecting to MySQL

Type

$appname

$appname : 

Name of Application that uses this class

Type

$debug

$debug : 

Debugstate, default is OFF.

Type

$classversion

$classversion : 

Class version.

Type

$querycounter

$querycounter : 

Counts processed queries against the database

Type

$querytime

$querytime : 

Contains the total SQL execution time in microseconds.

Type

$stmt

$stmt : 

Stores active statement handle.

Type

$myErrno

$myErrno : 

Error code of last mysql operation (set in Print_Error())

Type

$myErrStr

$myErrStr : 

Error string of last mysql operation (set in Print_Error())

Type

$AdminEmail

$AdminEmail : 

Email Address for the administrator of this project

Type

$SAPI_type

$SAPI_type : 

The SAPI type of php (used to detect CLI sapi)

Type

$currentQuery

$currentQuery : 

Contains the actual query to be processed.

Type

$showError

$showError : 

Flag indicates how the class should interact with errors

Type

$usePConnect

$usePConnect : 

Flag indicates if persistant connections should be used

Type

$characterset

$characterset : 

Character set to use.

Type

$locale

$locale : 

Character set to use.

Type

$num_rows

$num_rows : 

Number of rows from result set (or affected rows when using bind vars)

Type

Methods

__construct()

__construct(string  $extconfig = '') 

Constructor of class.

The constructor takes default values from dbdefs.inc.php. Please see this file for further informations about the setable options.

Parameters

string $extconfig

Optional other filename for dbdefs.inc.php, defaults to "dbdefs.inc.php".

Throws

\Exception

SetCompatMode()

SetCompatMode() 

Compatibility method to simulate old db_mysql class behavour.

You can enable this by setting the define MYSQLDB_COMPATIBLE_MODE to TRUE (defaults to FALSE).

Connect()

Connect(string  $user = '', string  $pass = '', string  $host = '', string  $db = '', integer  $port) : mixed

Performs the connection to MySQL.

If anything goes wrong calls Print_Error(). You should set the defaults for your connection by setting user,pass,host,port and database in dbdefs.inc.php and leave connect() parameters empty. If there is an active connection already stored internally this value is returned and no new connection is made. If MYSQLDB_CHARACTERSET define is set a "SET NAMES ''; is used straight after connecting.

Parameters

string $user

Username used to connect to DB.

string $pass

Password to use for given username.

string $host

Hostname of database to connect to.

string $db

Schema to use on MySQL DB Server.

integer $port

TCP port to use for connection. Defaults to 3306.

Returns

mixed —

Either the DB connection handle or NULL in case of an error.

Disconnect()

Disconnect(mixed  $other_sock = -1) 

Disconnects from MySQL.

You may optionally pass an external link identifier.

Parameters

mixed $other_sock

Optionally your own connection handle to close, else internal will be used.

Query()

Query(string  $querystring, integer  $resflag = MYSQLI_ASSOC, integer  $no_exit) : mixed

Performs a single-row query and returns result (if one exists).

Resflag can be MYSQLI_NUM or MYSQLI_ASSOC depending on what kind of array you want to be returned.

Parameters

string $querystring

The SQL query to send to database.

integer $resflag

Decides how the result should be returned:

  • MYSQLI_ASSOC = Data is returned as assoziative array
  • MYSQLI_NUM = Data is returned as numbered array
  • MYSQLI_BOTH = Data is returned as both numbered and associative array.
integer $no_exit

Decides how the class should react on errors. If you set this to 1 the class won't automatically exit on an error but instead return the mysqli_errno value. Default of 0 means that the class calls Print_Error() and exists.

Returns

mixed —

Either an array as result of the query or an error code or TRUE.

QueryResult()

QueryResult(string  $querystring, integer  $no_exit) : mixed

Performs a multi-row query and returns result identifier.

Parameters

string $querystring

The Query to be executed

integer $no_exit

The error indicator flag, can be one of:

  • 0 = (Default), In case of an error Print_Error is called and script terminates
  • 1 = In case of an error this function returns the error from mysqli_errno()

Returns

mixed —

A resource identifier or an errorcode (if $no_exit = 1)

FetchResult()

FetchResult(mixed  $result, integer  $resflag = MYSQLI_ASSOC) : array

Fetches next row from result handle.

Returns either numeric (MYSQLI_NUM) or associative (MYSQLI_ASSOC) array for one data row as pointed to by result var.

Parameters

mixed $result

The resource identifier as returned by QueryResult()

integer $resflag

How you want the data to be returned:

  • MYSQLI_ASSOC = Data is returned as assoziative array
  • MYSQLI_NUM = Data is returned as numbered array

Returns

array —

One row of the resulting query or NULL if there are no data anymore

FreeResult()

FreeResult(mixed  $result = NULL) : mixed

Frees result returned by QueryResult() and Prepare().

It is a good programming practise to give back what you have taken, so after processing your Multi-Row query with FetchResult() finally call this function to free the allocated memory.

Parameters

mixed $result

The resource identifier you want to be freed. If not given the internal statement handle is used.

Returns

mixed —

The resulting code of mysqli_free_result (can be ignored).

SetDebug()

SetDebug(integer  $state) 

Sets debug level for debugging of SQL Queries.

$state can have these values:

  • self::DBOF_DEBUG_OFF = Turn off debugging
  • self::DBOF_DEBUG_SCREEN = Turn on debugging on screen (every Query will be dumped on screen)
  • self::DBOF_DEBUG_FILE = Turn on debugging on PHP errorlog You can mix the debug levels by adding the according defines!

Parameters

integer $state

The DEBUG Level you want to be set

GetDebug()

GetDebug() : integer

Returns the current debug setting.

Returns

integer —

The debug setting (bitmask)

PrintDebug()

PrintDebug(string  $msg) 

Handles output according to internal debug flag.

Parameters

string $msg

The Text to be included in the debug message.

GetClassVersion()

GetClassVersion() : string

Returns version of this class.

Returns

string —

The version of this class.

Version()

Version() : string

Returns MySQL Server Version.

Opens an own connection if no active one exists.

Returns

string —

MySQL Server Version

GetQueryCount()

GetQueryCount() : integer

Returns amount of queries executed by this class.

Returns

integer —

Query counter

GetQueryTime()

GetQueryTime() : float

Returns amount of time spent on queries executed by this class.

Returns

float —

Time in seconds.msecs spent in executin MySQL code.

SetErrorHandling()

SetErrorHandling(integer  $val) 

Allows to set the class error reporting level in case of an error.

  • self::DBOF_SHOW_NO_ERRORS => Show no security-relevant informations
  • self::DBOF_SHOW_ALL_ERRORS => Show all errors (useful for develop)
  • self::DBOF_RETURN_ALL_ERRORS => No error/autoexit, just return the mysql error code.

Parameters

integer $val

The Error Handling mode you wish to use.

GetErrorHandling()

GetErrorHandling() : integer

Retrieve current error reporting level.

Returns

integer —

The Error Handling mode currently in use.

GetConnectionHandle()

GetConnectionHandle() : mixed

Returns current connection handle.

Returns either the internal connection socket or -1 if no active handle exists. Useful if you want to work with mysqli* functions in parallel to this class.

Returns

mixed —

Internal socket value

SetConnectionHandle()

SetConnectionHandle(mixed  $extsock) 

Allows to set internal socket to external value.

Parameters

mixed $extsock

New socket handle to set (as returned from mysqli_connect())

CheckSock()

CheckSock() 

Checks if we are already connected to our database.

If not terminates by calling Print_Error().

SetAutoCommit()

SetAutoCommit(boolean  $state, mixed  $extsock = -1) : boolean

Sets autocommit flag.

Parameters

boolean $state

TRUE = Autocommit enabled, FALSE = autocommit disabled.

mixed $extsock

Optional an external mysqli connect resource, default is internally stored resource.

Returns

boolean —

The returnvalue of mysqli_autocommit. FALSE in any case if no open link is found.

GetAutoCommit()

GetAutoCommit(mixed  $extsock = -1) : boolean

Retrieves autocommit flag.

Note that this only works for connected (!) databases, as we have to read the state from the server!

Parameters

mixed $extsock

Optional an external mysqli connect resource. Default is internally stored resource.

Returns

boolean —

TRUE = autocommit is enabled, FALSE = autocommit is disabled.

Commit()

Commit(mixed  $extsock = -1) : boolean

Commits the current transaction.

Parameters

mixed $extsock

Optional an external mysqli connect resource. Default is internally stored resource.

Returns

boolean —

The return value from mysqli_commit()

Rollback()

Rollback(mixed  $extsock = -1) : boolean

Rollback current transaction.

Parameters

mixed $extsock

Optional an external mysqli connect resource. Default is internally stored resource.

Returns

boolean —

The return value from mysqli_rollback()

LastInsertId()

LastInsertId(mixed  $extsock = -1) : integer

Returns last used auto_increment id.

Parameters

mixed $extsock

Optionally an external MySQL socket to use. If not given the internal socket is used.

Returns

integer —

The last automatic insert id that was assigned by the MySQL server.

AffectedRows()

AffectedRows(mixed  $extsock = -1) : integer

Returns count of affected rows by last DML operation.

Parameters

mixed $extsock

Optionally an external MySQLi socket to use. If not given the internal socket is used.

Returns

integer —

The number of affected rows by previous DML operation.

GetErrno()

GetErrno(mixed  $other_sock = -1) : integer

Retrieve last mysql error number.

Parameters

mixed $other_sock

Optionally your own connection handle to check, else internal will be used.

Returns

integer —

The MySQL error number of the last operation

GetErrorText()

GetErrorText(mixed  $other_sock = -1) : string

Retrieve last mysql error description.

Parameters

mixed $other_sock

Optionally your own connection handle to check, else internal will be used.

Returns

string —

The MySQL error description of the last operation

ConvertMySQLDate()

ConvertMySQLDate(string  $mysqldate, string  $fmtstring) : string

Converts a MySQL default Datestring (YYYY-MM-DD HH:MI:SS) into a strftime() compatible format.

You can use all format tags that strftime() supports, this function simply converts the mysql date string into a timestamp which is then passed to strftime together with your supplied format. The converted datestring is then returned. Please do not use this as default date converter, always use DATE_FORMAT() inside a query whenever possible as this is much faster than using this function! Only if you cannot use the MySQL SQL Date converting functions consider using this function.

Parameters

string $mysqldate

The MySQL default datestring in format YYYY-MM-DD HH:MI:SS

string $fmtstring

A strftime() compatible format string.

Returns

string —

The converted date string.

EscapeString()

EscapeString(string  $str) : string

Escapes a given string with the 'mysqli_real_escape_string' method.

Always use this function to avoid SQL injections when adding dynamic data to MySQL! This function also handles the settings for magic_quotes_gpc/magic_quotes_sybase, if these settings are enabled this function uses stripslashes() first.

Parameters

string $str

The string to escape.

Returns

string —

The escaped string.

Set_TimeNames()

Set_TimeNames(string  $locale) : integer

Method to set the time_names setting of the MySQL Server.

Pass it a valid locale string to change the locale setting of MySQL. Note that this is supported only since 5.0.25 of MySQL!

Parameters

string $locale

A locale string for the language you want to set, i.e. 'de_DE'.

Returns

integer —

0 If an error occures or 1 if change was successful.

Get_TimeNames()

Get_TimeNames() : string

Method to return the current MySQL setting for the lc_time_names variable.

Returns

string —

The current setting for the lc_time_names variable.

Set_CharSet()

Set_CharSet(string  $charset) : integer

Method to set the character set of the current connection.

You must specify a valid character set name, else the class will report an error. See http://dev.mysql.com/doc/refman/5.0/en/charset-charsets.html for a list of supported character sets.

Parameters

string $charset

The charset to set on the MySQL server side.

Returns

integer —

1 If all works, else 0 in case of an error.

Get_CharSet()

Get_CharSet() : array

Method to return the current MySQL setting for the character_set variables.

Note that MySQL returns a list of settings, so this method returns all character_set related settings as an associative array.

Returns

array —

The current settings for the character_set variables.

DescTable()

DescTable(string  $tname) : array

Returns the description for a given table.

Array returned has the following fields: 0 => name of the field 1 => type of field 2 => The fieldsize 3 => Field flags (like auto_increment).

Parameters

string $tname

Name of table to describe.

Returns

array —

Numerical array with all required table informations.

NumRows()

NumRows() : integer

Returns the number of rows in the result set.

Use this after a SELECT or SHOW etc. command has been executed. For DML operations like INSERT, UPDATE, DELETE the method AffectedRows() has to be used.

Returns

integer —

Number of affected rows.

PerformNewInsert()

PerformNewInsert(string  $table_name,   $fields, string  $sql = 'INSERT') : array|boolean

Function performs an insert statement from a given variable list.

The insert statements will be constructed as NEW Insert style, and aligned to the "max_allowed_packet" boundary. This can dramatically improve bulk-inserts compared to fire every INSERT statement one by one. The array passed must be constructed with the keys defined as fieldnames and the values as the corresponding values. Looks like this:

  • $data[0]['fieldname1'] = 'wert0/1';
  • $data[0]['fieldname2'] = 'wert0/2';
  • $data[1]['fieldname1'] = 'wert1/2';
  • $data[1]['fieldname2'] = 'wert1/2';

NOTE: Database must be connected!

Parameters

string $table_name

Name of table to perform the insert against.

$fields
string $sql

Either "INSERT" or "REPLACE", no other command is allowed here!

Returns

array|boolean —

A numeric array with [0] => created query count, [1] => Querysize or FALSE in case of an error.

SetPConnect()

SetPConnect(boolean  $conntype) : boolean

Sets connection behavour.

If FALSE class uses mysqli_connect to connect. If TRUE class uses mysqli_connect to connect with prefix "p:" (Persistant connection).

Parameters

boolean $conntype

The new setting for persistant connections.

Returns

boolean —

The previous state.

GetPConnect()

GetPConnect() : boolean

Returns current persistant connection flag.

Returns

boolean —

The current setting (TRUE/FALSE).

Prepare()

Prepare(string  $sql) : mixed

Prepares a SQL statement so that the variables can be bound afterwards.

Returns the statement handle or FALSE in case of an error.

Parameters

string $sql

The Query to prepare

Returns

mixed —

Either the valid statement handle or FALSE in case of an error.

Execute()

Execute(\mysqli_stmt  $stmt, integer  $no_exit, array  $bindvars = null) : boolean|integer|\mysqli_result|null

Executes an prepared statement and optionally bind variables for bindvar-enabled queries.

See examples/test_bind_vars.php for a working example.

Parameters

\mysqli_stmt $stmt

The statement handle as returned by Prepare()

integer $no_exit

Decides how the class should react on errors. If you set this to 1 the class won't automatically exit on an error but instead return the mysqli_errno value.

array $bindvars

Associative array with variables to bind via mysqli_stmt_bind_param().

Returns

boolean|integer|\mysqli_result|null

QueryHash()

QueryHash(string  $querystring, integer  $resflag = MYSQLI_ASSOC, integer  $no_exit,   $bindvars = null) : mixed

Single query method with Bind var support.

Resflag can be "MYSQLI_NUM" or "MYSQLI_ASSOC" depending on what kind of array you want to be returned.

Parameters

string $querystring

The SQL query to send to database.

integer $resflag

Decides how the result should be returned:

  • MYSQLI_ASSOC = Data is returned as assoziative array
  • MYSQLI_NUM = Data is returned as numbered array
  • MYSQLI_BOTH = Data is returned as both numbered and associative array.
integer $no_exit

Decides how the class should react on errors. If you set this to 1 the class won't automatically exit on an error but instead return the mysqli_errno value.

$bindvars

Returns

mixed —

Either an array as result of the query or an error code or TRUE.

QueryResultHash()

QueryResultHash(string  $querystring, integer  $no_exit,   $bindvars = null) : mixed

Multirow query method with Bind var support.

Resflag can be "MYSQLI_NUM" or "MYSQLI_ASSOC" depending on what kind of array you want to be returned.

Parameters

string $querystring

The SQL query to send to database.

integer $no_exit

Decides how the class should react on errors. If you set this to 1 the class won't automatically exit on an error but instead return the mysqli_errno value.

$bindvars

Returns

mixed —

Either an array as result of the query or an error code or TRUE.

Print_Error()

Print_Error(string  $ustr = "", mixed  $var2dump = "", integer  $exit_on_error = 1) : mixed

Prints out MySQL Error in own <div> container and exits.

Please note that this function does not return as long as you have not set db_mysqli::DBOF_RETURN_ALL_ERRORS!

Parameters

string $ustr

User-defined Error string to show

mixed $var2dump

Optionally a variable to print out with print_r()

integer $exit_on_error

If set to default of 1 this function terminates execution of the script by calling exit, else it simply returns.

Returns

mixed —

Depends on exit_on_error setup either error number or null

getmicrotime()

getmicrotime() : float

Returns microtime in format s.mmmmm.

Used to measure SQL execution time.

Returns

float —

the current time in microseconds.

SendMailOnError()

SendMailOnError(integer  $merrno, string  $merrstr, string  $uerrstr) 

Send error email if programmer has defined a valid email address and enabled it with the define MYSQLDB_SENTMAILONERROR.

Parameters

integer $merrno

MySQL errno number

string $merrstr

MySQL error description

string $uerrstr

User-supplied error description