dolibarr 21.0.0-beta
DoliDBMysqli Class Reference

Class to manage Dolibarr database access for a MySQL database using the MySQLi extension. More...

Inheritance diagram for DoliDBMysqli:
Collaboration diagram for DoliDBMysqli:

Public Member Functions

 __construct ($type, $host, $user, $pass, $name='', $port=0)
 Constructor.
 
 hintindex ($nameofindex, $mode=1)
 Return SQL string to force an index.
 
 convertSQLFromMysql ($line, $type='ddl')
 Convert a SQL request in Mysql syntax to native syntax.
 
 select_db ($database)
 Select a database.
 
 connect ($host, $login, $passwd, $name, $port=0)
 Connect to server.
 
 getVersion ()
 Return version of database server.
 
 getDriverInfo ()
 Return version of database client driver.
 
 close ()
 Close database connection.
 
 query ($query, $usesavepoint=0, $type='auto', $result_mode=0)
 Execute a SQL request and return the resultset.
 
 fetch_object ($resultset)
 Returns the current line (as an object) for the resultset cursor.
 
 fetch_array ($resultset)
 Return data as an array.
 
 fetch_row ($resultset)
 Return data as an array.
 
 num_rows ($resultset)
 Return number of lines for result of a SELECT.
 
 affected_rows ($resultset)
 Return the number of lines in the result of a request INSERT, DELETE or UPDATE.
 
 free ($resultset=null)
 Libere le dernier resultset utilise sur cette connection.
 
 escape ($stringtoencode)
 Escape a string to insert data.
 
 escapeforlike ($stringtoencode)
 Escape a string to insert data into a like.
 
 errno ()
 Return generic error code of last operation.
 
 error ()
 Return description of last error.
 
 last_insert_id ($tab, $fieldid='rowid')
 Get last ID after an insert INSERT.
 
 encrypt ($fieldorvalue, $withQuotes=1)
 Encrypt sensitive data in database Warning: This function includes the escape and add the SQL simple quotes on strings.
 
 decrypt ($value)
 Decrypt sensitive data in database.
 
 DDLGetConnectId ()
 Return connection ID.
 
 DDLCreateDb ($database, $charset='', $collation='', $owner='')
 Create a new database Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated We force to create database with charset this->forcecharset and collate this->forcecollate.
 
 DDLListTables ($database, $table='')
 List tables into a database.
 
 DDLListTablesFull ($database, $table='')
 List tables into a database.
 
 DDLInfoTable ($table)
 List information of columns in a table.
 
 DDLCreateTable ($table, $fields, $primary_key, $type, $unique_keys=null, $fulltext_keys=null, $keys=null)
 Create a table into database.
 
 DDLDropTable ($table)
 Drop a table into database.
 
 DDLDescTable ($table, $field="")
 Return a pointer of line with description of a table or field.
 
 DDLAddField ($table, $field_name, $field_desc, $field_position="")
 Create a new field into table.
 
 DDLUpdateField ($table, $field_name, $field_desc)
 Update format of a field into a table.
 
 DDLDropField ($table, $field_name)
 Drop a field from table.
 
 DDLCreateUser ($dolibarr_main_db_host, $dolibarr_main_db_user, $dolibarr_main_db_pass, $dolibarr_main_db_name)
 Create a user and privileges to connect to database (even if database does not exists yet)
 
 getDefaultCharacterSetDatabase ()
 Return charset used to store data in current database Note: if we are connected to databasename, it is same result than using SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = "databasename";)
 
 getListOfCharacterSet ()
 Return list of available charset that can be used to store data in database.
 
 getDefaultCollationDatabase ()
 Return collation used in current database.
 
 getListOfCollation ()
 Return list of available collation that can be used for database.
 
 getPathOfDump ()
 Return full path of dump program.
 
 getPathOfRestore ()
 Return full path of restore program.
 
 getServerParametersValues ($filter='')
 Return value of server parameters.
 
 getServerStatusValues ($filter='')
 Return value of server status (current indicators on memory, cache...)
 
- Public Member Functions inherited from DoliDB
 prefix ()
 Return the DB prefix found into prefix_db (if it was set manually by doing $dbhandler->prefix_db=...).
 
 ifsql ($test, $resok, $resko)
 Format a SQL IF.
 
 stddevpop ($nameoffield)
 Return SQL string to aggregate using the Standard Deviation of population.
 
 regexpsql ($subject, $pattern, $sqlstring=0)
 Format a SQL REGEXP.
 
 idate ($param, $gm='tzserver')
 Convert (by PHP) a GM Timestamp date into a string date with PHP server TZ to insert into a date field.
 
 lasterrno ()
 Return last error code.
 
 sanitize ($stringtosanitize, $allowsimplequote=0, $allowsequals=0, $allowsspace=0, $allowschars=1)
 Sanitize a string for SQL forging.
 
 begin ($textinlog='')
 Start transaction.
 
 commit ($log='')
 Validate a database transaction.
 
 rollback ($log='')
 Cancel a transaction and go back to initial data values.
 
 plimit ($limit=0, $offset=0)
 Define limits and offset of request.
 
 getVersionArray ()
 Return version of database server into an array.
 
 lastquery ()
 Return last request executed with query()
 
 order ($sortfield='', $sortorder='')
 Define sort criteria of request.
 
 lasterror ()
 Return last error label.
 
 jdate ($string, $gm='tzserver')
 Convert (by PHP) a PHP server TZ string date into a Timestamps date (GMT if gm=true) 19700101020000 -> 3600 with server TZ = +1 and $gm='tzserver' 19700101020000 -> 7200 whatever is server TZ if $gm='gmt'.
 
 lastqueryerror ()
 Return last query in error.
 
 getRow ($sql)
 Return first result from query as object Note : This method executes a given SQL query and retrieves the first row of results as an object.
 
 getRows ($sql)
 Return all results from query as an array of objects.
 
- Public Member Functions inherited from Database
 sanitize ($stringtosanitize)
 Sanitize a string for SQL forging.
 

Public Attributes

 $type = 'mysqli'
 Database type.
 
const LABEL = 'MySQL or MariaDB'
 Database label.
 
const VERSIONMIN = '5.0.3'
 Version min database.
 
- Public Attributes inherited from DoliDB
const VERSIONMIN = self::VERSIONMIN
 Force subclass to implement VERSIONMIN - required DB version.
 
const LABEL = self::LABEL
 Force subclass to implement LABEL - description of DB type.
 

Static Protected Member Functions

static getCallerInfoString ()
 Get caller info.
 

Detailed Description

Class to manage Dolibarr database access for a MySQL database using the MySQLi extension.

Definition at line 36 of file mysqli.class.php.

Constructor & Destructor Documentation

◆ __construct()

DoliDBMysqli::__construct ( $type,
$host,
$user,
$pass,
$name = '',
$port = 0 )

Constructor.

This create an opened connection to a database server and eventually to a database

Parameters
string$typeType of database (mysql, pgsql...). Not used.
string$hostAddress of database server
string$userName of database user
string$passPassword of database user
string$nameName of database
int$portPort of database server

Definition at line 62 of file mysqli.class.php.

References $conf, connect(), dol_syslog(), error(), and select_db().

Member Function Documentation

◆ affected_rows()

DoliDBMysqli::affected_rows ( $resultset)

Return the number of lines in the result of a request INSERT, DELETE or UPDATE.

Parameters
mysqli_result$resultsetCurseur de la requete voulue
Returns
int Number of lines
See also
num_rows()

Implements Database.

Definition at line 482 of file mysqli.class.php.

◆ close()

DoliDBMysqli::close ( )

Close database connection.

Returns
bool True if disconnect successful, false otherwise
See also
connect()

Implements Database.

Definition at line 307 of file mysqli.class.php.

References dol_syslog().

◆ connect()

DoliDBMysqli::connect ( $host,
$login,
$passwd,
$name,
$port = 0 )

Connect to server.

Parameters
string$hostDatabase server host
string$loginLogin
string$passwdPassword
string$nameName of database (not used for mysql, used for pgsql)
integer$portPort of database server
Returns
mysqli|mysqliDoli|false Database access object
See also
close()

Implements Database.

Definition at line 257 of file mysqli.class.php.

References dol_print_error(), and dol_syslog().

Referenced by __construct().

◆ convertSQLFromMysql()

DoliDBMysqli::convertSQLFromMysql ( $line,
$type = 'ddl' )

Convert a SQL request in Mysql syntax to native syntax.

Parameters
string$lineSQL request line to convert
string$typeType of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
Returns
string SQL request line converted

Implements Database.

Definition at line 219 of file mysqli.class.php.

◆ DDLAddField()

DoliDBMysqli::DDLAddField ( $table,
$field_name,
$field_desc,
$field_position = "" )

Create a new field into table.

Parameters
string$tableName of table
string$field_nameName of field to add
array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string}$field_desc Associative table with description of field to insert [parameter name][parameter value]
string$field_positionOptional e.g.: "after some_field"
Returns
int Return integer <0 if KO, >0 if OK

Implements Database.

Definition at line 969 of file mysqli.class.php.

◆ DDLCreateDb()

DoliDBMysqli::DDLCreateDb ( $database,
$charset = '',
$collation = '',
$owner = '' )

Create a new database Do not use function xxx_create_db (xxx=mysql, ...) as they are deprecated We force to create database with charset this->forcecharset and collate this->forcecollate.

Parameters
string$databaseDatabase name to create
string$charsetCharset used to store data
string$collationCharset used to sort data
string$ownerUsername of database owner
Returns
null|mysqli_result Resource defined if OK, null if KO

Implements Database.

Definition at line 707 of file mysqli.class.php.

◆ DDLCreateTable()

DoliDBMysqli::DDLCreateTable ( $table,
$fields,
$primary_key,
$type,
$unique_keys = null,
$fulltext_keys = null,
$keys = null )

Create a table into database.

Parameters
string$tableName of table
array<string,array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int<-2,5>|string,noteditable?:int<0,1>,default?:string,index?:int,foreignkey?:string,searchall?:int<0,1>,isameasure?:int<0,1>,css?:string,csslist?:string,help?:string,showoncombobox?:int<0,2>,disabled?:int<0,1>,arrayofkeyval?:array<int|string,string>,comment?:string,validate?:int<0,1>}>$fields Tableau associatif [nom champ][tableau des descriptions]
string$primary_keyNom du champ qui sera la clef primaire
string$typeType de la table
?array<string,mixed>$unique_keys Tableau associatifs Nom de champs qui seront clef unique => valeur
string[]$fulltext_keysTableau des Nom de champs qui seront indexes en fulltext
array<string,mixed>$keys Tableau des champs cles noms => valeur
Returns
int Return integer <0 if KO, >=0 if OK

Implements Database.

Definition at line 836 of file mysqli.class.php.

◆ DDLCreateUser()

DoliDBMysqli::DDLCreateUser ( $dolibarr_main_db_host,
$dolibarr_main_db_user,
$dolibarr_main_db_pass,
$dolibarr_main_db_name )

Create a user and privileges to connect to database (even if database does not exists yet)

Parameters
string$dolibarr_main_db_hostIp server or ''
string$dolibarr_main_db_userNom new user
string$dolibarr_main_db_passPassword for the new user
string$dolibarr_main_db_nameDatabase name where user must be granted
Returns
int Return integer <0 if KO, >=0 if OK

Implements Database.

Definition at line 1086 of file mysqli.class.php.

◆ DDLDescTable()

DoliDBMysqli::DDLDescTable ( $table,
$field = "" )

Return a pointer of line with description of a table or field.

Parameters
string$tableName of table
string$fieldOptionnel : Name of field if we want description of field
Returns
bool|mysqli_result Resultset x (x->Field, x->Type, ...)

Implements Database.

Definition at line 949 of file mysqli.class.php.

◆ DDLDropField()

DoliDBMysqli::DDLDropField ( $table,
$field_name )

Drop a field from table.

Parameters
string$tableName of table
string$field_nameName of field to drop
Returns
int Return integer <0 if KO, >0 if OK

Implements Database.

Definition at line 1062 of file mysqli.class.php.

◆ DDLDropTable()

DoliDBMysqli::DDLDropTable ( $table)

Drop a table into database.

Parameters
string$tableName of table
Returns
int Return integer <0 if KO, >=0 if OK

Implements Database.

Definition at line 927 of file mysqli.class.php.

◆ DDLGetConnectId()

DoliDBMysqli::DDLGetConnectId ( )

Return connection ID.

Returns
string Id connection

Implements Database.

Definition at line 683 of file mysqli.class.php.

◆ DDLInfoTable()

DoliDBMysqli::DDLInfoTable ( $table)

List information of columns in a table.

Parameters
string$tableName of table
Returns
array<array<mixed>> Table with information of columns in the table

Implements Database.

Definition at line 804 of file mysqli.class.php.

◆ DDLListTables()

DoliDBMysqli::DDLListTables ( $database,
$table = '' )

List tables into a database.

Parameters
string$databaseName of database
string$tableName of table filter ('xxx')
Returns
string[] List of tables in an array

Implements Database.

Definition at line 741 of file mysqli.class.php.

◆ DDLListTablesFull()

DoliDBMysqli::DDLListTablesFull ( $database,
$table = '' )

List tables into a database.

Parameters
string$databaseName of database
string$tableName of table filter ('xxx')
Returns
array<array{0:string,1:string}> List of tables in an array

Implements Database.

Definition at line 773 of file mysqli.class.php.

◆ DDLUpdateField()

DoliDBMysqli::DDLUpdateField ( $table,
$field_name,
$field_desc )

Update format of a field into a table.

Parameters
string$tableName of table
string$field_nameName of field to modify
array{type:string,label:string,enabled:int<0,2>|string,position:int,notnull?:int,visible:int,noteditable?:int,default?:string,index?:int,foreignkey?:string,searchall?:int,isameasure?:int,css?:string,csslist?:string,help?:string,showoncombobox?:int,disabled?:int,arrayofkeyval?:array<int,string>,comment?:string,value?:string,null?:string}$field_desc Array with description of field format
Returns
int Return integer <0 if KO, >0 if OK

Implements Database.

Definition at line 1017 of file mysqli.class.php.

◆ decrypt()

DoliDBMysqli::decrypt ( $value)

Decrypt sensitive data in database.

Parameters
string$valueValue to decrypt
Returns
string Decrypted value if used

Implements Database.

Definition at line 653 of file mysqli.class.php.

References $conf.

◆ encrypt()

DoliDBMysqli::encrypt ( $fieldorvalue,
$withQuotes = 1 )

Encrypt sensitive data in database Warning: This function includes the escape and add the SQL simple quotes on strings.

Parameters
string$fieldorvalueField name or value to encrypt
int$withQuotesReturn string including the SQL simple quotes. This param must always be 1 (Value 0 is bugged and deprecated).
Returns
string XXX(field) or XXX('value') or field or 'value'

Implements Database.

Definition at line 624 of file mysqli.class.php.

References $conf, and escape().

◆ errno()

DoliDBMysqli::errno ( )

Return generic error code of last operation.

Returns
string Error code (Examples: DB_ERROR_TABLE_ALREADY_EXISTS, DB_ERROR_RECORD_ALREADY_EXISTS...)

Implements Database.

Definition at line 540 of file mysqli.class.php.

Referenced by query().

◆ error()

DoliDBMysqli::error ( )

Return description of last error.

Returns
string Error text

Implements Database.

Definition at line 592 of file mysqli.class.php.

Referenced by __construct(), and query().

◆ escape()

DoliDBMysqli::escape ( $stringtoencode)

Escape a string to insert data.

Parameters
string$stringtoencodeString to escape
Returns
string String escaped

Implements Database.

Definition at line 518 of file mysqli.class.php.

Referenced by encrypt().

◆ escapeforlike()

DoliDBMysqli::escapeforlike ( $stringtoencode)

Escape a string to insert data into a like.

Parameters
string$stringtoencodeString to escape
Returns
string String escaped

Implements Database.

Definition at line 529 of file mysqli.class.php.

◆ fetch_array()

DoliDBMysqli::fetch_array ( $resultset)

Return data as an array.

Parameters
mysqli_result$resultsetResultset of request
Returns
array<int|string,mixed>|null|false Array or null if KO or end of cursor

Implements Database.

Definition at line 424 of file mysqli.class.php.

◆ fetch_object()

DoliDBMysqli::fetch_object ( $resultset)

Returns the current line (as an object) for the resultset cursor.

Parameters
mysqli_result$resultsetCurseur de la requete voulue
Returns
object|null Object result line or null if KO or end of cursor

Implements Database.

Definition at line 406 of file mysqli.class.php.

◆ fetch_row()

DoliDBMysqli::fetch_row ( $resultset)

Return data as an array.

Parameters
mysqli_result$resultsetResultset of request
Returns
array<int,mixed>|null|int<0,0> Array or null if KO or end of cursor or 0 if resultset is bool

Implements Database.

Definition at line 441 of file mysqli.class.php.

◆ free()

DoliDBMysqli::free ( $resultset = null)

Libere le dernier resultset utilise sur cette connection.

Parameters
mysqli_result$resultsetCurseur de la requete voulue
Returns
void

Implements Database.

Definition at line 500 of file mysqli.class.php.

◆ getCallerInfoString()

static DoliDBMysqli::getCallerInfoString ( )
staticfinalprotected

Get caller info.

Returns
string

Definition at line 386 of file mysqli.class.php.

◆ getDefaultCharacterSetDatabase()

DoliDBMysqli::getDefaultCharacterSetDatabase ( )

Return charset used to store data in current database Note: if we are connected to databasename, it is same result than using SELECT default_character_set_name FROM information_schema.SCHEMATA WHERE schema_name = "databasename";)

Returns
string Charset
See also
getDefaultCollationDatabase()

Implements Database.

Definition at line 1131 of file mysqli.class.php.

◆ getDefaultCollationDatabase()

DoliDBMysqli::getDefaultCollationDatabase ( )

Return collation used in current database.

Returns
string Collation value
See also
getDefaultCharacterSetDatabase()

Implements Database.

Definition at line 1174 of file mysqli.class.php.

◆ getDriverInfo()

DoliDBMysqli::getDriverInfo ( )

Return version of database client driver.

Returns
string Version string

Implements Database.

Definition at line 295 of file mysqli.class.php.

◆ getListOfCharacterSet()

DoliDBMysqli::getListOfCharacterSet ( )

Return list of available charset that can be used to store data in database.

Returns
?array<int,array{charset:string,description:string}> List of Charset

Implements Database.

Definition at line 1149 of file mysqli.class.php.

◆ getListOfCollation()

DoliDBMysqli::getListOfCollation ( )

Return list of available collation that can be used for database.

Returns
?array<int,array{collation:string}> List of Collations

Implements Database.

Definition at line 1192 of file mysqli.class.php.

◆ getPathOfDump()

DoliDBMysqli::getPathOfDump ( )

Return full path of dump program.

Returns
string Full path of dump program

Implements Database.

Definition at line 1215 of file mysqli.class.php.

◆ getPathOfRestore()

DoliDBMysqli::getPathOfRestore ( )

Return full path of restore program.

Returns
string Full path of restore program

Implements Database.

Definition at line 1233 of file mysqli.class.php.

◆ getServerParametersValues()

DoliDBMysqli::getServerParametersValues ( $filter = '')

Return value of server parameters.

Parameters
string$filterFilter list on a particular value
Returns
array<string,string> Array of key-values (key=>value)

Implements Database.

Definition at line 1252 of file mysqli.class.php.

◆ getServerStatusValues()

DoliDBMysqli::getServerStatusValues ( $filter = '')

Return value of server status (current indicators on memory, cache...)

Parameters
string$filterFilter list on a particular value
Returns
array<string,string> Array of key-values (key=>value)

Implements Database.

Definition at line 1276 of file mysqli.class.php.

◆ getVersion()

DoliDBMysqli::getVersion ( )

Return version of database server.

Returns
string Version string

Implements Database.

Definition at line 285 of file mysqli.class.php.

◆ hintindex()

DoliDBMysqli::hintindex ( $nameofindex,
$mode = 1 )

Return SQL string to force an index.

Parameters
string$nameofindexName of index
int$mode0=Use, 1=Force
Returns
string SQL string

Reimplemented from DoliDB.

Definition at line 206 of file mysqli.class.php.

◆ last_insert_id()

DoliDBMysqli::last_insert_id ( $tab,
$fieldid = 'rowid' )

Get last ID after an insert INSERT.

Parameters
string$tabTable name concerned by insert. Ne sert pas sous MySql mais requis pour compatibilite avec Postgresql
string$fieldidField name
Returns
int|string Id of row

Implements Database.

Definition at line 610 of file mysqli.class.php.

◆ num_rows()

DoliDBMysqli::num_rows ( $resultset)

Return number of lines for result of a SELECT.

Parameters
mysqli_result$resultsetResulset of requests
Returns
int Nb of lines
See also
affected_rows()

Implements Database.

Definition at line 464 of file mysqli.class.php.

◆ query()

DoliDBMysqli::query ( $query,
$usesavepoint = 0,
$type = 'auto',
$result_mode = 0 )

Execute a SQL request and return the resultset.

Parameters
string$querySQL query string
int$usesavepoint0=Default mode, 1=Run a savepoint before and a rollback to savepoint if error (this allow to have some request with errors inside global transactions). Note that with Mysql, this parameter is not used as Myssql can already commit a transaction even if one request is in error, without using savepoints.
string$typeType of SQL order ('ddl' for insert, update, select, delete or 'dml' for create, alter...)
int$result_modeResult mode (Using 1=MYSQLI_USE_RESULT instead of 0=MYSQLI_STORE_RESULT will not buffer the result and save memory)
Returns
false|mysqli_result Resultset of answer

Implements Database.

Definition at line 331 of file mysqli.class.php.

References dol_syslog(), errno(), error(), getDolGlobalInt(), DoliDB\lasterrno(), DoliDB\lasterror(), DoliDB\lastquery(), and DoliDB\lastqueryerror().

◆ select_db()

DoliDBMysqli::select_db ( $database)

Select a database.

Parameters
string$databaseName of database
Returns
boolean true if OK, false if KO

Implements Database.

Definition at line 232 of file mysqli.class.php.

References dol_syslog().

Referenced by __construct().


The documentation for this class was generated from the following file: