PHP Class MySQL

Inheritance: implements DB
Afficher le fichier Open project: kimai/kimai Class Usage Examples

Méthodes publiques

Свойство Type Description
$ThrowExceptions Determines if an error throws an exception
$mysql_link class-internal variables - do not change

Méthodes publiques

Méthode Description
AutoInsertUpdate ( string $tableName, array $valuesArray, array $whereArray ) : boolean Automatically does an INSERT or UPDATE depending if an existing record exists in a table
BeginningOfSeek ( ) : boolean Returns true if the internal pointer is at the beginning of the records
BuildSQLDelete ( string $tableName, array $whereArray = null ) : string [STATIC] Builds a SQL DELETE statement
BuildSQLInsert ( string $tableName, array $valuesArray ) : string [STATIC] Builds a SQL INSERT statement
BuildSQLSelect ( string $tableName, array $whereArray = null, array/string $columns = null, array/string $sortColumns = null, boolean $sortAscending = true, integer/string $limit = null ) : string Builds a simple SQL SELECT statement
BuildSQLUpdate ( string $tableName, array $valuesArray, array $whereArray = null ) : string [STATIC] Builds a SQL UPDATE statement
BuildSQLWhereClause ( array $whereArray ) : string [STATIC] Builds a SQL WHERE clause from an array.
Close ( ) : object Close current MySQL connection
DeleteRows ( string $tableName, array $whereArray = null ) : boolean Deletes rows in a table based on a WHERE filter (can be just one or many rows based on the filter)
EndOfSeek ( ) : boolean Returns true if the internal pointer is at the end of the records
Error ( ) : string Returns the last MySQL error as text
ErrorNumber ( ) : integer Returns the last MySQL error as a number
GetBooleanValue ( mixed $value ) : boolean [STATIC] Converts any value of any datatype into boolean (true or false)
GetColumnComments ( string $table ) : array Returns the comments for fields in a table into an array or NULL if the table has not got any fields
GetColumnCount ( string $table = "" ) : integer This function returns the number of columns or returns FALSE on error
GetColumnDataType ( string $column, string $table = "" ) : string This function returns the data type for a specified column. If the column does not exists or no records exist, it returns FALSE
GetColumnID ( string $column, string $table = "" ) : integer This function returns the position of a column
GetColumnLength ( string $column, string $table = "" ) : integer This function returns the field length or returns FALSE on error
GetColumnName ( string $columnID, string $table = "" ) : integer This function returns the name for a specified column number. If the index does not exists or no records exist, it returns FALSE
GetColumnNames ( string $table = "" ) : array Returns the field names in a table or query in an array
GetHTML ( boolean $showCount = true, string $styleTable = null, string $styleHeader = null, string $styleData = null ) : string This function returns the last query as an HTML table
GetJSON ( ) : string Returns the last query as a JSON document
GetLastInsertID ( ) : integer Returns the last autonumber ID field from a previous INSERT query
GetLastSQL ( ) : string Returns the last SQL statement executed
GetTables ( ) : array This function returns table names from the database into an array. If the database does not contains any tables, the returned value is FALSE
GetXML ( ) : string Returns the last query as an XML Document
HasRecords ( string $sql = "" ) : boolean Determines if a query contains any rows
InsertRow ( string $tableName, array $valuesArray ) : integer Inserts a row into a table in the connected database
IsConnected ( ) : boolean Determines if a valid connection to the database exists
IsDate ( date/string $value ) : boolean [STATIC] Determines if a value of any data type is a date PHP can convert
Kill ( $message = "" ) Stop executing (die/exit) and show last MySQL error message
MoveFirst ( ) : boolean Seeks to the beginning of the records
MoveLast ( ) : boolean Seeks to the end of the records
Open ( string $database = null, string $server = null, string $username = null, string $password = null, string $charset = null, boolean $pcon = false ) : boolean Connect to specified MySQL server
Query ( string $sql ) : object Executes the given SQL query and returns the records
QueryArray ( string $sql, integer $resultType = MYSQLI_BOTH ) : array Executes the given SQL query and returns a multi-dimensional array
QuerySingleRow ( string $sql ) : object Executes the given SQL query and returns only one (the first) row
QuerySingleRowArray ( string $sql, integer $resultType = MYSQLI_BOTH ) : array Executes the given SQL query and returns the first row as an array
QuerySingleValue ( string $sql ) : mixed Executes a query and returns a single value. If more than one row is returned, only the first value in the first column is returned.
QueryTimed ( string $sql ) : object Executes the given SQL query, measures it, and saves the total duration in microseconds
Records ( ) : object Returns the records from the last query
RecordsArray ( integer $resultType = MYSQLI_BOTH ) : Records Returns all records from last query and returns contents as array or FALSE on error
Release ( ) : boolean Frees memory used by the query results and returns the function result
Row ( integer $optional_row_number = null ) : object Reads the current row and returns contents as a PHP object or returns false on error
RowArray ( integer $optional_row_number = null, integer $resultType = MYSQLI_BOTH ) : array Reads the current row and returns contents as an array or returns false on error
RowCount ( ) : integer Returns the last query row count
SQLBooleanValue ( mixed $value, mixed $trueValue, mixed $falseValue, string $datatype = self::SQLVALUE_TEXT ) : string [STATIC] Converts a boolean into a formatted TRUE or FALSE value of choice
SQLFix ( string $value ) : string Returns string suitable for SQL NOTE: This is no longer a static call because of a conflict with mysqli_real_escape_string.
SQLValue ( mixed $value, string $datatype = self::SQLVALUE_TEXT ) : string [STATIC] Formats any value into a string suitable for SQL statements (NOTE: Also supports data types returned from the gettype function)
Seek ( integer $row_number ) : object Sets the internal database pointer to the specified row number and returns the result
SeekPosition ( ) : integer Returns the current cursor row location
SelectDatabase ( string $database, string $charset = "" ) : boolean Selects a different database and character set
SelectRows ( string $tableName, array $whereArray = null, array/string $columns = null, array/string $sortColumns = null, boolean $sortAscending = true, integer/string $limit = null ) : boolean Gets rows in a table based on a WHERE filter
SelectTable ( string $tableName ) : boolean Retrieves all rows in a specified table
TimerDuration ( integer $decimals = 4 ) : Float Returns last measured duration (time between TimerStart and TimerStop)
TimerStart ( ) Starts time measurement (in microseconds)
TimerStop ( ) Stops time measurement (in microseconds)
TransactionBegin ( ) : boolean Starts a transaction
TransactionEnd ( ) : boolean Ends a transaction and commits the queries
TransactionRollback ( ) : boolean Rolls the transaction back
TruncateTable ( string $tableName ) : boolean Truncates a table removing all data
UpdateRows ( string $tableName, array $valuesArray, array $whereArray = null ) : boolean Updates rows in a table based on a WHERE filter (can be just one or many rows based on the filter)
__construct ( boolean $connect = true, string $database = null, string $server = null, string $username = null, string $password = null, string $charset = null ) Constructor: Opens the connection to the database
__destruct ( ) Destructor: Closes the connection to the database

Private Methods

Méthode Description
BuildSQLColumns ( $columns, boolean $addQuotes = true, boolean $showAlias = true ) : string [STATIC] Builds a comma delimited list of columns for use with SQL
ResetError ( ) Clears the internal variables from any error information
SetError ( string $errorMessage = "", integer $errorNumber ) Sets the local variables with the last error information

Method Details

AutoInsertUpdate() public méthode

Automatically does an INSERT or UPDATE depending if an existing record exists in a table
public AutoInsertUpdate ( string $tableName, array $valuesArray, array $whereArray ) : boolean
$tableName string The name of the table
$valuesArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect)
$whereArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect).
Résultat boolean Returns TRUE on success or FALSE on error

BeginningOfSeek() public méthode

Returns true if the internal pointer is at the beginning of the records
public BeginningOfSeek ( ) : boolean
Résultat boolean TRUE if at the first row or FALSE if not

BuildSQLDelete() public static méthode

[STATIC] Builds a SQL DELETE statement
public static BuildSQLDelete ( string $tableName, array $whereArray = null ) : string
$tableName string The name of the table
$whereArray array (Optional) An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect). If not specified then all values in the table are deleted.
Résultat string Returns the SQL DELETE statement

BuildSQLInsert() public static méthode

[STATIC] Builds a SQL INSERT statement
public static BuildSQLInsert ( string $tableName, array $valuesArray ) : string
$tableName string The name of the table
$valuesArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect)
Résultat string Returns a SQL INSERT statement

BuildSQLSelect() public static méthode

Builds a simple SQL SELECT statement
public static BuildSQLSelect ( string $tableName, array $whereArray = null, array/string $columns = null, array/string $sortColumns = null, boolean $sortAscending = true, integer/string $limit = null ) : string
$tableName string The name of the table
$whereArray array (Optional) An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect)
$columns array/string
$sortColumns array/string
$sortAscending boolean (Optional) TRUE for ascending; FALSE for descending This only works if $sortColumns are specified
$limit integer/string
Résultat string Returns a SQL SELECT statement

BuildSQLUpdate() public static méthode

[STATIC] Builds a SQL UPDATE statement
public static BuildSQLUpdate ( string $tableName, array $valuesArray, array $whereArray = null ) : string
$tableName string The name of the table
$valuesArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect)
$whereArray array (Optional) An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect). If not specified then all values in the table are updated.
Résultat string Returns a SQL UPDATE statement

BuildSQLWhereClause() public static méthode

If a key is specified, the key is used at the field name and the value as a comparison. If a key is not used, the value is used as the clause.
public static BuildSQLWhereClause ( array $whereArray ) : string
$whereArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect)
Résultat string Returns a string containing the SQL WHERE clause

Close() public méthode

Close current MySQL connection
public Close ( ) : object
Résultat object Returns TRUE on success or FALSE on error

DeleteRows() public méthode

Deletes rows in a table based on a WHERE filter (can be just one or many rows based on the filter)
public DeleteRows ( string $tableName, array $whereArray = null ) : boolean
$tableName string The name of the table
$whereArray array (Optional) An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect). If not specified then all values in the table are deleted.
Résultat boolean Returns TRUE on success or FALSE on error

EndOfSeek() public méthode

Returns true if the internal pointer is at the end of the records
public EndOfSeek ( ) : boolean
Résultat boolean TRUE if at the last row or FALSE if not

Error() public méthode

Returns the last MySQL error as text
public Error ( ) : string
Résultat string Error text from last known error

ErrorNumber() public méthode

Returns the last MySQL error as a number
public ErrorNumber ( ) : integer
Résultat integer Error number from last known error

GetBooleanValue() public static méthode

[STATIC] Converts any value of any datatype into boolean (true or false)
public static GetBooleanValue ( mixed $value ) : boolean
$value mixed Value to analyze for TRUE or FALSE
Résultat boolean Returns TRUE or FALSE

GetColumnComments() public méthode

Returns the comments for fields in a table into an array or NULL if the table has not got any fields
public GetColumnComments ( string $table ) : array
$table string Table name
Résultat array An array that contains the column comments

GetColumnCount() public méthode

This function returns the number of columns or returns FALSE on error
public GetColumnCount ( string $table = "" ) : integer
$table string (Optional) If a table name is not specified, the column count is returned from the last query
Résultat integer The total count of columns

GetColumnDataType() public méthode

This function returns the data type for a specified column. If the column does not exists or no records exist, it returns FALSE
public GetColumnDataType ( string $column, string $table = "" ) : string
$column string Column name or number (first column is 0)
$table string (Optional) If a table name is not specified, the last returned records are used
Résultat string MySQL data (field) type

GetColumnID() public méthode

This function returns the position of a column
public GetColumnID ( string $column, string $table = "" ) : integer
$column string Column name
$table string (Optional) If a table name is not specified, the last returned records are used.
Résultat integer Column ID

GetColumnLength() public méthode

This function returns the field length or returns FALSE on error
public GetColumnLength ( string $column, string $table = "" ) : integer
$column string Column name
$table string (Optional) If a table name is not specified, the last returned records are used.
Résultat integer Field length

GetColumnName() public méthode

This function returns the name for a specified column number. If the index does not exists or no records exist, it returns FALSE
public GetColumnName ( string $columnID, string $table = "" ) : integer
$columnID string Column position (0 is the first column)
$table string (Optional) If a table name is not specified, the last returned records are used.
Résultat integer Field Length

GetColumnNames() public méthode

Returns the field names in a table or query in an array
public GetColumnNames ( string $table = "" ) : array
$table string (Optional) If a table name is not specified, the last returned records are used
Résultat array An array that contains the column names

GetHTML() public méthode

This function returns the last query as an HTML table
public GetHTML ( boolean $showCount = true, string $styleTable = null, string $styleHeader = null, string $styleData = null ) : string
$showCount boolean (Optional) TRUE if you want to show the row count, FALSE if you do not want to show the count
$styleTable string (Optional) Style information for the table
$styleHeader string (Optional) Style information for the header row
$styleData string (Optional) Style information for the cells
Résultat string HTML containing a table with all records listed

GetJSON() public méthode

Returns the last query as a JSON document
public GetJSON ( ) : string
Résultat string JSON containing all records listed

GetLastInsertID() public méthode

Returns the last autonumber ID field from a previous INSERT query
public GetLastInsertID ( ) : integer
Résultat integer ID number from previous INSERT query

GetLastSQL() public méthode

Returns the last SQL statement executed
public GetLastSQL ( ) : string
Résultat string Current SQL query string

GetTables() public méthode

This function returns table names from the database into an array. If the database does not contains any tables, the returned value is FALSE
public GetTables ( ) : array
Résultat array An array that contains the table names

GetXML() public méthode

Returns the last query as an XML Document
public GetXML ( ) : string
Résultat string XML containing all records listed

HasRecords() public méthode

Determines if a query contains any rows
public HasRecords ( string $sql = "" ) : boolean
$sql string [Optional] If specified, the query is first executed Otherwise, the last query is used for comparison
Résultat boolean TRUE if records exist, FALSE if not or query error

InsertRow() public méthode

Inserts a row into a table in the connected database
public InsertRow ( string $tableName, array $valuesArray ) : integer
$tableName string The name of the table
$valuesArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect)
Résultat integer Returns last insert ID on success or FALSE on failure

IsConnected() public méthode

Determines if a valid connection to the database exists
public IsConnected ( ) : boolean
Résultat boolean TRUE idf connectect or FALSE if not connected

IsDate() public static méthode

[STATIC] Determines if a value of any data type is a date PHP can convert
public static IsDate ( date/string $value ) : boolean
$value date/string
Résultat boolean Returns TRUE if value is date or FALSE if not date

Kill() public méthode

Stop executing (die/exit) and show last MySQL error message
public Kill ( $message = "" )

MoveFirst() public méthode

Seeks to the beginning of the records
public MoveFirst ( ) : boolean
Résultat boolean Returns TRUE on success or FALSE on error

MoveLast() public méthode

Seeks to the end of the records
public MoveLast ( ) : boolean
Résultat boolean Returns TRUE on success or FALSE on error

Open() public méthode

Connect to specified MySQL server
public Open ( string $database = null, string $server = null, string $username = null, string $password = null, string $charset = null, boolean $pcon = false ) : boolean
$database string (Optional) Database name
$server string (Optional) Host address
$username string (Optional) User name
$password string (Optional) Password
$charset string (Optional) Character set
$pcon boolean (Optional) Persistant connection
Résultat boolean Returns TRUE on success or FALSE on error

Query() public méthode

Executes the given SQL query and returns the records
public Query ( string $sql ) : object
$sql string The query string should not end with a semicolon
Résultat object PHP 'mysql result' resource object containing the records on SELECT, SHOW, DESCRIBE or EXPLAIN queries and returns; TRUE or FALSE for all others i.e. UPDATE, DELETE, DROP AND FALSE on all errors (setting the local Error message)

QueryArray() public méthode

Executes the given SQL query and returns a multi-dimensional array
public QueryArray ( string $sql, integer $resultType = MYSQLI_BOTH ) : array
$sql string The query string should not end with a semicolon
$resultType integer (Optional) The type of array Values can be: MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH
Résultat array A multi-dimensional array containing all the data returned from the query or FALSE on all errors

QuerySingleRow() public méthode

Executes the given SQL query and returns only one (the first) row
public QuerySingleRow ( string $sql ) : object
$sql string The query string should not end with a semicolon
Résultat object PHP resource object containing the first row or FALSE if no row is returned from the query

QuerySingleRowArray() public méthode

Executes the given SQL query and returns the first row as an array
public QuerySingleRowArray ( string $sql, integer $resultType = MYSQLI_BOTH ) : array
$sql string The query string should not end with a semicolon
$resultType integer (Optional) The type of array Values can be: MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH
Résultat array An array containing the first row or FALSE if no row is returned from the query

QuerySingleValue() public méthode

Executes a query and returns a single value. If more than one row is returned, only the first value in the first column is returned.
public QuerySingleValue ( string $sql ) : mixed
$sql string The query string should not end with a semicolon
Résultat mixed The value returned or FALSE if no value

QueryTimed() public méthode

Executes the given SQL query, measures it, and saves the total duration in microseconds
public QueryTimed ( string $sql ) : object
$sql string The query string should not end with a semicolon
Résultat object PHP 'mysql result' resource object containing the records on SELECT, SHOW, DESCRIBE or EXPLAIN queries and returns TRUE or FALSE for all others i.e. UPDATE, DELETE, DROP

Records() public méthode

Returns the records from the last query
public Records ( ) : object
Résultat object PHP 'mysql result' resource object containing the records for the last query executed

RecordsArray() public méthode

Returns all records from last query and returns contents as array or FALSE on error
public RecordsArray ( integer $resultType = MYSQLI_BOTH ) : Records
$resultType integer (Optional) The type of array Values can be: MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH
Résultat Records in array form

Release() public méthode

Frees memory used by the query results and returns the function result
public Release ( ) : boolean
Résultat boolean Returns TRUE on success or FALSE on failure

Row() public méthode

Reads the current row and returns contents as a PHP object or returns false on error
public Row ( integer $optional_row_number = null ) : object
$optional_row_number integer (Optional) Use to specify a row
Résultat object PHP object or FALSE on error

RowArray() public méthode

Reads the current row and returns contents as an array or returns false on error
public RowArray ( integer $optional_row_number = null, integer $resultType = MYSQLI_BOTH ) : array
$optional_row_number integer (Optional) Use to specify a row
$resultType integer (Optional) The type of array Values can be: MYSQLI_ASSOC, MYSQLI_NUM, MYSQLI_BOTH
Résultat array Array that corresponds to fetched row or FALSE if no rows

RowCount() public méthode

Returns the last query row count
public RowCount ( ) : integer
Résultat integer Row count or FALSE on error

SQLBooleanValue() public static méthode

[STATIC] Converts a boolean into a formatted TRUE or FALSE value of choice
public static SQLBooleanValue ( mixed $value, mixed $trueValue, mixed $falseValue, string $datatype = self::SQLVALUE_TEXT ) : string
$value mixed value to analyze for TRUE or FALSE
$trueValue mixed value to use if TRUE
$falseValue mixed value to use if FALSE
$datatype string Use SQLVALUE constants or the strings: string, text, varchar, char, boolean, bool, Y-N, T-F, bit, date, datetime, time, integer, int, number, double, float
Résultat string SQL formatted value of the specified data type

SQLFix() public méthode

Also, please note that the SQLUnfix function was removed for compatibility.
public SQLFix ( string $value ) : string
$value string
Résultat string SQL formatted value

SQLValue() public static méthode

[STATIC] Formats any value into a string suitable for SQL statements (NOTE: Also supports data types returned from the gettype function)
public static SQLValue ( mixed $value, string $datatype = self::SQLVALUE_TEXT ) : string
$value mixed Any value of any type to be formatted to SQL
$datatype string Use SQLVALUE constants or the strings: string, text, varchar, char, boolean, bool, Y-N, T-F, bit, date, datetime, time, integer, int, number, double, float
Résultat string

Seek() public méthode

Sets the internal database pointer to the specified row number and returns the result
public Seek ( integer $row_number ) : object
$row_number integer Row number
Résultat object Fetched row as PHP object

SeekPosition() public méthode

Returns the current cursor row location
public SeekPosition ( ) : integer
Résultat integer Current row number

SelectDatabase() public méthode

Selects a different database and character set
public SelectDatabase ( string $database, string $charset = "" ) : boolean
$database string Database name
$charset string (Optional) Character set (i.e. utf8)
Résultat boolean Returns TRUE on success or FALSE on error

SelectRows() public méthode

Gets rows in a table based on a WHERE filter
public SelectRows ( string $tableName, array $whereArray = null, array/string $columns = null, array/string $sortColumns = null, boolean $sortAscending = true, integer/string $limit = null ) : boolean
$tableName string The name of the table
$whereArray array (Optional) An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect)
$columns array/string
$sortColumns array/string
$sortAscending boolean (Optional) TRUE for ascending; FALSE for descending This only works if $sortColumns are specified
$limit integer/string
Résultat boolean Returns records on success or FALSE on error

SelectTable() public méthode

Retrieves all rows in a specified table
public SelectTable ( string $tableName ) : boolean
$tableName string The name of the table
Résultat boolean Returns records on success or FALSE on error

TimerDuration() public méthode

Returns last measured duration (time between TimerStart and TimerStop)
public TimerDuration ( integer $decimals = 4 ) : Float
$decimals integer (Optional) The number of decimal places to show
Résultat Float Microseconds elapsed

TimerStart() public méthode

Starts time measurement (in microseconds)
public TimerStart ( )

TimerStop() public méthode

Stops time measurement (in microseconds)
public TimerStop ( )

TransactionBegin() public méthode

Starts a transaction
public TransactionBegin ( ) : boolean
Résultat boolean Returns TRUE on success or FALSE on error

TransactionEnd() public méthode

Ends a transaction and commits the queries
public TransactionEnd ( ) : boolean
Résultat boolean Returns TRUE on success or FALSE on error

TransactionRollback() public méthode

Rolls the transaction back
public TransactionRollback ( ) : boolean
Résultat boolean Returns TRUE on success or FALSE on failure

TruncateTable() public méthode

Truncates a table removing all data
public TruncateTable ( string $tableName ) : boolean
$tableName string The name of the table
Résultat boolean Returns TRUE on success or FALSE on error

UpdateRows() public méthode

Updates rows in a table based on a WHERE filter (can be just one or many rows based on the filter)
public UpdateRows ( string $tableName, array $valuesArray, array $whereArray = null ) : boolean
$tableName string The name of the table
$valuesArray array An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect)
$whereArray array (Optional) An associative array containing the column names as keys and values as data. The values must be SQL ready (i.e. quotes around strings, formatted dates, ect). If not specified then all values in the table are updated.
Résultat boolean Returns TRUE on success or FALSE on error

__construct() public méthode

Constructor: Opens the connection to the database
public __construct ( boolean $connect = true, string $database = null, string $server = null, string $username = null, string $password = null, string $charset = null )
$connect boolean (Optional) Auto-connect when object is created
$database string (Optional) Database name
$server string (Optional) Host address
$username string (Optional) User name
$password string (Optional) Password
$charset string (Optional) Character set

__destruct() public méthode

Destructor: Closes the connection to the database
public __destruct ( )

Property Details

$ThrowExceptions public_oe property

Determines if an error throws an exception
public $ThrowExceptions