Class: Zebra_Databasesource file: /Zebra_Database.phpA compact, lightweight yet feature-rich PHP MySQLi database wrapper providing methods for interacting with MySQL databases that are more secure, powerful and intuitive than PHP's default ones. Can provides debugging information when called from CLI (command-line interface) and supports logging queries done through AJAX requests. Read more here Author(s):
Version:
License:Copyright:
Properties
Methods
Class propertiesinteger $affected_rowsThe number of rows affected after running an See the returned_rows property for getting the number of rows returned by
boolean $auto_quote_replacements = truestring $cache_pathPath where to store cached queries results. The path must be relative to your working path and not the path of this library! topstring $caching_method = 'disk'The method to be used for caching query results. Can be either:
Caching is done on a per-query basis by setting the Warnings related to the presence of memcache and redis can be disabled by setting the disable_warnings property. Default is Tags
boolean | string | array
|
since: | turning debugging on/off via a query string is available since 2.10.0 |
An array of IP addresses for which to show the debugging console / write to log file, if the debug property is not set to FALSE
.
Default is an empty array
since: | 1.0.6 |
Enables logging of queries done through AJAX requests.
When set to TRUE
all AJAX requests sent by your page will be captured and checked for logs generated by Zebra Database
.
Note that when this is enabled HTML code will be appended to the result of the AJAX requests!
Therefore, depending on your use-case, having this enabled could break some of your page's functionality.
Default is FALSE
since: | 2.11.0 |
If debugging is enabled on the fly via the presence of a query string argument (see the debug property), a cookie is set so that the query string is not required to be present in subsequent requests.
This property sets that cookie's name.
Default value is zebra_db
since: | 2.10.0 |
Indicates whether backtrace information should be shown in the debugging console.
Default is TRUE
since: | 2.5.9 |
Show a link for editing the query in your favorite database manager like (like phpMyAdmin or Adminer).
It should be an HTML anchor (a link) to your favorite database manager where you build the query string according to the requirements of said database manager using the following placeholders:
...were any of those placeholders will be replaced by the values you have passed on to connect, except for the %query% placeholder which will be replaced by the respective query.
Here's how to set up the value for opening the query in Adminer:
I don't use phpMyAdmin so if you manage to set it up, please share the result with me so I can add it to the docs. Thanks!
Be VERY CAREFUL when using this feature and make sure you do not expose your credentials in the links you build.
Setting it to false
will disable the feature.
Default is a link to the documentation on how to set the link up.
since: | 2.11.0 |
Indicates whether queries should be EXPLAINed in the debugging console.
Default is TRUE
since: | 2.5.9 |
Indicates which of $_REQUEST
, $_POST
, $_GET
, $_SESSION
, $_COOKIE
, $_FILES
and $_SERVER
superglobals should be available in the debugging console, under the globals section.
Can be set to either boolean TRUE
or FALSE
as a global setting, or as an associative array where each option's visibility can be individually be set, like in the example below:
Default is TRUE
since: | 2.9.14 |
Sets the number of records returned by SELECT
queries to be shown in the debugging console.
Setting this to 0
or FALSE
will disable this feature.
Be aware that having this property set to a high number (hundreds) and having queries that di return that many rows can cause your script to crash due to memory limitations. In this case you should either lower the value of this property or try and set PHP's memory limit higher using:
Default is 20
since: | 1.0.9 |
By default, if set_charset() method is not called, caching is used and memcache or redis are available but none of them is used, a warning message will be displayed in the debugging console.
The ensure that data is both properly saved and retrieved to and from the database, this method should be called first thing after connecting to a database.
If you don't want to call this method nor do you want to see the warning, set this property to TRUE
Default is FALSE
After running a SELECT
query through select(), query() or query_unbuffered() methods and having the calc_rows argument set to TRUE
, this property will contain the number of records that would have been returned if there was no LIMIT
applied to the query.
If calc_rows is FALSE
, or it is TRUE
but there is no LIMIT
applied to the query, this property's value will be the same as the value of the returned_rows property.
For unbuffered queries the value of this property will be available only after iterating over all the records with either fetch_assoc() or fetch_obj() methods. Until then, the value will be 0!
When the value of this property is set to TRUE
, the execution of the script will be halted after the first unsuccessful query and the debugging console will be shown (or debug information will be written to the log file if configured so), if the value of the debug property is not FALSE
and the viewer's IP address is in the debugger_ip array (or debugger_ip is an empty array).
If you want to stop on errors no matter what, set the value of this property to always
and that will raise an exception regardless of the value of the debug property.
If you ever consider using this with its value set to always
, I recommend using the debug property with a string value instead.
Default is TRUE
since: | 1.0.5 |
Path where to store the log files when the debug property is set to an array OR a callback function to pass the log information to instead of being written to a file.
The path is relative to your working directory.
Use .
(dot) for the current directory instead of an empty string or the log file will be written to the server's root.
If a full path is specified (including an extension) the log file's name will be used from there. Otherwise, the log file's name will be log.txt
At the given path the library will attempt to create a file named "log.txt" (or variations as described here) so the appropriate rights will need to be granted to the script!
IF YOU'RE LOGGING, MAKE SURE YOU HAVE A CRON JOB THAT DELETES THE LOG FILES FROM TIME TO TIME!
Default is ""
(an empty string) - log files are created in the root of your server.
If you are using a callback function, the function receives the following arguments:
FALSE
, this will be an empty stringTime (in seconds) after which a query will be considered as running for too long.
If a query's execution time exceeds this number a notification email will be automatically sent to the address defined by notification_address, having notifier_domain in subject.
Default is 10
Setting this property to TRUE
will instruct to library to compress the cached results (using zlib
).
For this to work, PHP needs to be configured with --with-zlib[=DIR]
!
Set this property only if you are using memcache
as caching_method.
Default is FALSE
since: | 2.7 |
The host where the memcache server is listening for connections.
Set this property only if you are using memcache
as caching_method.
Default is FALSE
since: | 2.7 |
The prefix for the keys used to identify cached queries in memcache. This allows separate caching of the same queries by multiple instances of the libraries, or the same instance handling multiple domains on the same memcache server.
Set this property only if you are using memcache
as caching_method.
Default is ""
(an empty string)
since: | 2.8.4 |
The port on which the memcache server is listening for connections.
Set this property only if you are using memcache
as caching_method.
Default is FALSE
since: | 2.7 |
By setting this property to TRUE
a minimized version of the debugging console will be shown by default instead of the full-sized one.
Clicking on it will show the full debugging console.
For quick and easy debugging, setting the highlight
argument of a method that has it will result in the debugging console being shown at full size and with the respective query visible for inspecting.
Default is TRUE
since: | 1.0.4 |
Email address to which notification emails to be sent when a query's execution time exceeds the number of seconds set by max_query_time. The notification email will be automatically sent to the address defined by notification_address and having notifier_domain in subject.
Mails are sent using PHP's mail function.
Domain name to be used in the subject of notification emails sent when a query's execution time exceeds the number of seconds set by max_query_time.
If a query's execution time exceeds the number of seconds set by max_query_time, a notification email will be automatically sent to the address defined by notification_address and having notifier_domain in subject.
Mails are sent using PHP's mail function.
Setting this property to TRUE
will instruct to library to compress the cached results (using zlib
).
For this to work, PHP needs to be configured with --with-zlib[=DIR]
!
Set this property only if you are using redis
as caching_method.
Default is FALSE
since: | 2.10.0 |
The host where the redis server is listening for connections.
Set this property only if you are using redis
as caching_method.
Default is FALSE
since: | 2.10.0 |
The prefix for the keys used to identify cached queries in redis. This allows separate caching of the same queries by multiple instances of the libraries, or the same instance handling multiple domains on the same redis server.
Set this property only if you are using redis
as caching_method.
Default is ""
(an empty string)
since: | 2.10.0 |
The port on which the redis server is listening for connections.
Set this property only if you are using redis
as caching_method.
Default is FALSE
since: | 2.10.0 |
Path to parent of public folder containing the css
and javascript
folders.
The path must be relative to your $_SERVER['DOCUMENT_ROOT']
After running a SELECT
query through select(), query() or query_unbuffered() methods, this property will contain the number of returned rows.
For unbuffered queries the value of this property will be available only after iterating over all the records with either fetch_assoc() or fetch_obj() methods. Until then, the value will be 0!
See found_rows also.
since: | 1.0.4 |
boolean close ( [ boolean $reset_options = false ] )
Closes the MySQL connection and optionally unsets the connection options previously set with the option() method
boolean | $reset_options | If set to Default is This option was added in 2.9.5 |
return: | Returns TRUE on success or FALSE on failure |
since: | 1.1.0 |
void connect ( string $host , string $user , string $password , [ string $database = '' ] , [ string $port = '' ] , [ string $socket = '' ] , [ boolean $connect = false ] )
Opens a connection to a MySQL server and optionally selects a database
Since the library is using lazy connection (it is not actually connecting to the database until the first query is executed), the object representing the connection to the MySQL server is not available at this time. In case you need it before running any queries, use the get_link() method.
If you want the connection to the database to be made right away, set the connect
argument to TRUE
string | $host | The address of the MySQL server to connect to (i.e. localhost) Prepending |
string | $user | The user name used for authentication when connecting to the MySQL server |
string | $password | The password used for authentication when connecting to the MySQL server |
string | $database | (Optional) The database to be selected after the connection is established This can also be set later with the select_database() method Default is |
string | $port | (Optional) The port number to use when connecting to the MySQL server Default is |
string | $socket | (Optional) The socket or named pipe that should be used. Specifying the socket parameter will not explicitly determine the type of connection to be used when connecting to the MySQL server. How the connection is made to the MySQL database is determined by the host argument. Default is |
boolean | $connect | (Optional) Setting this argument to Default is |
mixed dcount ( string $column , string $table , [ string $where = '' ] , [ mixed $replacements = '' ] , [ mixed $cache = false ] , [ boolean $highlight = false ] )
Counts the values in a column of a table
string | $column | Name of the column in which to do the counting |
string | $table | Name of the table containing the column May also be specified like |
string | $where | (Optional) A MySQL Default is |
mixed | $replacements | (Optional) An array with as many items as the total parameter markers ( Default is |
mixed | $cache | (Optional) Instructs the library on whether it should cache the query results or not. Can be either The caching method is specified through the caching_method property. Default is |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns the number of counted records or This method may return boolean |
boolean delete ( string $table , [ string $where = '' ] , [ mixed $replacements = '' ] , [ boolean $highlight = false ] )
Deletes rows from a table
string | $table | Table from which to delete May also be specified like |
string | $where | (Optional) A MySQL Default is |
mixed | $replacements | (Optional) An array with as many items as the total parameter markers ( Default is |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns TRUE on success or FALSE on error |
since: | 1.0.9 |
mixed dlookup ( string $column , string $table , [ string $where = '' ] , [ mixed $replacements = '' ] , [ mixed $cache = false ] , [ boolean $highlight = false ] )
Returns one or more columns from ONE row of a table
string | $column | One or more columns to return data from If only one column is specified, the returned result will be the specified column's value. If more columns are specified the returned result will be an associative array! You may use |
string | $table | Name of the table in which to search May also be specified like |
string | $where | (Optional) A MySQL Default is |
mixed | $replacements | (Optional) An array with as many items as the total parameter markers ( Default is |
mixed | $cache | (Optional) Instructs the library on whether it should cache the query results or not. Can be either The caching method is specified through the caching_method property. Default is |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns the found value/values or FALSE if no records matching the given criteria (if any) were found. It also returns FALSE if there are no records in the table, or if there was an error. |
mixed dmax ( string $column , string $table , [ string $where = '' ] , [ mixed $replacements = '' ] , [ mixed $cache = false ] , [ boolean $highlight = false ] )
Looks up the maximum value in a column of a table
string | $column | Name of the column in which to search |
string | $table | Name of table in which to search May also be specified like |
string | $where | (Optional) A MySQL Default is |
mixed | $replacements | (Optional) An array with as many items as the total parameter markers ( Default is |
mixed | $cache | (Optional) Instructs the library on whether it should cache the query results or not. Can be either The caching method is specified through the caching_method property. Default is |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns the maximum value in the column or This method may return boolean |
mixed dsum ( string $column , string $table , [ string $where = '' ] , [ mixed $replacements = '' ] , [ mixed $cache = false ] , [ boolean $highlight = false ] )
Sums the values in a column of a table
string | $column | Name of the column in which to sum values |
string | $table | Name of the table in which to search May also be specified like |
string | $where | (Optional) A MySQL Default is |
mixed | $replacements | (Optional) An array with as many items as the total parameter markers ( Default is |
mixed | $cache | (Optional) Instructs the library on whether it should cache the query results or not. Can be either The caching method is specified through the caching_method property. Default is |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns the summed values or This method may return boolean |
mixed error ( [ boolean $return_error_number = false ] )
Returns the description of the last error, or an empty string if no error occurred
In most cases you should not need this method as any errors are shown in the debugging console as long as the debugging is on, or available in PHP's error log file (if your environment is configured to do so) when set to FALSE
.
If, for some reasons, none of the above is available, you can use this method to see errors.
boolean | $return_error_number | Indicates whether the error number should also be returned. If set to
...or an empty string if no error occurred. Default is |
return: | Returns the description of the last error, or an empty string if no error occurred. |
since: | 2.9.1 |
string|boolean escape ( string $string )
Escapes special characters in a string making it safe to be used in an MySQL statement, in order to prevent SQL injections
This method also encloses given string in single quotes!
Works even if magic_quotes is ON.
string | $string | String to be quoted and escaped |
return: | Returns the original string enclosed in single quotes and with special characters escaped in order to prevent SQL injections, or FALSE if there's no database connection. |
mixed fetch_assoc ( [ resource|string $resource = '' ] )
Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead. The data is taken from the resource created by the previous query or from the resource given as argument.
resource|string | $resource | (Optional) Resource to fetch If not specified, the resource returned by the last run query is used |
return: | Returns an associative array that corresponds to the fetched row and moves the internal data pointer ahead, or FALSE if there are no more rows. |
mixed fetch_assoc_all ( [ int|string $index = '' ] , [ resource|string $resource = '' ] )
Returns an associative array containing all the rows from the resource created by the previous query or from the resource given as argument and moves the internal pointer to the end
int|string | $index | (Optional) Name of a column containing unique values If specified, the returned associative array's keys will be the values from this column. If not specified, returned array will have numerical indexes, starting from 0. |
resource|string | $resource | (Optional) Resource to fetch If not specified, the resource returned by the last run query is used |
return: | Returns an associative array containing all the rows from the resource created by the previous query or from the resource given as argument and moves the internal pointer to the end. Returns FALSE on error. |
since: | 1.1.2 |
mixed fetch_obj ( [ resource|string $resource = '' ] )
Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead.
The data is taken from the resource created by the previous query or from the resource given as argument.
resource|string | $resource | (Optional) Resource to fetch If not specified, the resource returned by the last run query is used |
return: | Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead, or FALSE if there are no more rows. |
since: | 1.0.8 |
mixed fetch_obj_all ( [ int|string $index = '' ] , [ resource|string $resource = '' ] )
Returns an associative array containing all the rows (as objects) from the resource created by the previous query or from the resource given as argument and moves the internal pointer to the end
int|string | $index | (Optional) A column name from the records, containing unique values If specified, the returned associative array's keys will be the values from this column. If not specified, returned array will have numerical indexes, starting from 0. |
resource|string | $resource | (Optional) Resource to fetch If not specified, the resource returned by the last run query is used |
return: | Returns an associative array containing all the rows (as objects) from the resource created by the previous query or from the resource given as argument and moves the internal pointer to the end. Returns FALSE on error. |
since: | 1.1.2 |
false|void free_result ( [ resource|string $resource = '' ] )
Frees the memory associated with a result
You should always free your result with free_result(), when your result object is not needed anymore
resource|string | $resource | (Optional) A valid resource If not specified, the resource returned by the last run query is used |
since: | 2.9.1 |
mixed get_columns ( [ resource|string $resource = '' ] )
Returns an array of associative arrays with information about the columns in the MySQL result associated with the specified result identifier
Each entry will have the column's name as key and, associated, an array with the following keys:
resource|string | $resource | (Optional) Resource to fetch columns information from. If not specified, the resource returned by the last run query is used |
return: | Returns an associative array with information about the columns in the MySQL result associated with the specified result identifier, or FALSE on error. |
since: | 2.0 |
object|boolean get_link ()
Returns the MySQL link identifier associated with the current connection to the MySQL server
Why a separate method? Because the library uses lazy connection (it is not actually connecting to the database until the first query is executed) there's no link identifier available when calling the connect() method.
return: | Returns the MySQL link identifier associated with the current connection to the MySQL server. |
since: | 2.5 |
mixed get_selected_database ()
Returns the name of the currently selected database
return: | Returns the name of the currently selected database, or FALSE if there's no active connection. |
since: | 2.8.7 |
array
Returns an array with all the tables in a database
string | $database | (Optional) The name of the database from which to return the names of existing tables If not specified, the tables from the currently selected database will be returned. This option was added in 2.9.5 |
return: | Returns an array with all the tables in the specified database |
since: | 1.1.2 |
array
Returns information about the columns of a given table as an associative array
string | $table | Name of table to return column information for May also be specified like |
return: | Returns information about the columns of a given table as an associative array. |
since: | 2.6 |
array
Returns an associative array with a lot of useful information on all or specific tables only
string | $table | (Optional) Table for which to return information for May also be specified like
If not specified, information will be returned for all the tables in the currently selected database. |
return: | Returns an associative array with a lot of useful information on all or specific tables only |
since: | 1.1.2 |
string
implode
(
array
Works similarly to PHP's implode()
function with the difference that the glue is always the comma and that all the values are automatically escaped
This was useful for escaping an array's values to be used in an SQL statements having the IN
keyword, before adding arrays directly in the replacement array became possible in version 2.8.6
array |
$items | An array with items to be glued together |
return: | Returns the string representation of all the array elements in the same order, escaped and with commas between each element. |
since: | 2.0 |
boolean insert ( string $table , mixed $columns , [ mixed $update = true ] , [ boolean $highlight = false ] )
Shorthand for INSERT
queries with additional IGNORE / ON DUPLICATE KEY
support
This method inserts a single row of data. For inserting multiple rows of data see the insert_bulk() method
When using this method column names will be automatically escaped in order to prevent SQL injections and will be enclosed in `
(grave accents), allowing the usage of reserved words.
string | $table | Table in which to insert May also be specified like |
mixed | $columns | An associative array where the array's keys represent the columns names and the array's values represent the values to be inserted in each respective column. Column names will be enclosed in grave accents You may also use any of MySQL's functions as values. Be aware that when using MySQL functions, the value will be used as it is, without being escaped! While this is ok when using a function without any arguments like |
mixed | $update | (Optional) By default, calling this method with this argument set to boolean Setting this argument to boolean Setting this argument to an array of column names will create a query where, on duplicate key, these given columns will be updated with their respective values from the $values argument. Alternatively, this argument can also be an associative array where the array's keys represent column names and the array's values represent the values to update the columns' values to if the inserted row would cause a duplicate key. Column names will be enclosed in grave accents You may also use any of MySQL's functions as values. Be aware that when using MySQL functions, the value will be used as it is, without being escaped! While this is ok when using a function without any arguments like For more information see MySQL's INSERT ... ON DUPLICATE KEY syntax. Default is |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns TRUE on success of FALSE on error. |
since: | 1.0.9 |
boolean insert_bulk ( string $table , mixed $columns , mixed $values , [ mixed $update = true ] , [ boolean $highlight = false ] )
Shorthand for inserting multiple rows in a single query with additional IGNORE / ON DUPLICATE KEY
support
When using this method column names will be enclosed in grave accents `
(thus allowing the usage of reserved words) and will be automatically escaped in order to prevent SQL injections.
string | $table | Table in which to insert May also be specified like |
mixed | $columns | An array with columns to insert values into Column names will be enclosed in grave accents |
mixed | $values | An array of an unlimited number of arrays with values to be inserted. The arrays must have the same number of items as you in the $columns argument. Values will be automatically escaped in order to prevent SQL injections. You may also use any of MySQL's functions as values. Be aware that when using MySQL functions, the value will be used as it is, without being escaped! While this is ok when using a function without any arguments like |
mixed | $update | (Optional) By default, calling this method with this argument set to boolean Setting this argument to boolean Setting this argument to an array of column names will create a query where, on duplicate key, these given columns will be updated with their respective values from the $values argument. Alternatively, this argument can also be an associative array where the array's keys represent column names and the array's values represent the values to update the columns' values to if the inserted row would cause a duplicate key. Column names will be enclosed in grave accents You may also use any of MySQL's functions as values. Be aware that when using MySQL functions, the value will be used as it is, without being escaped! While this is ok when using a function without any arguments like For more information see MySQL's INSERT ... ON DUPLICATE KEY syntax. Default is |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns TRUE on success of FALSE on error |
since: | 2.1 |
mixed insert_id ()
Retrieves the ID generated for an AUTO_INCREMENT
column by the previous INSERT
query
return: | Returns the ID generated for an AUTO_INCREMENT column by the previous INSERT query on success, 0 if the previous query did not generate an AUTO_INCREMENT value, or FALSE if there was no MySQL connection. |
since: | 1.0.4 |
boolean insert_update ( string $table , mixed $columns , [ mixed $update = array() ] , [ boolean $highlight = false ] )
This method is deprecated since 2.9.12 and will be removed in 3.0. Please use the insert_bulk() method
When using this method, if an inserted row would cause a duplicate value in a UNIQUE
index or PRIMARY KEY
, an UPDATE
of the existing row is performed.
Read more here.
When using this method column names will be enclosed in grave accents `
(thus allowing the usage of reserved words) and will be automatically escaped in order to prevent SQL injections.
string | $table | Table in which to insert/update May also be specified like |
mixed | $columns | An associative array where the array's keys represent the columns names and the array's values represent the values to be inserted in each respective column. Column names will be enclosed in grave accents You may also use any of MySQL's functions as values. Be aware that when using MySQL functions, the value will be used as it is, without being escaped! While this is ok when using a function without any arguments like |
mixed | $update | (Optional) An associative array where the array's keys represent the columns names and the array's values represent the values to update the columns' values to. This array represents the columns/values to be updated if the inserted row would cause a duplicate value in a If an empty array is given, the values in $columns will be used. Column names will be enclosed in grave accents A special value may also be used for when a column's value needs to be incremented or decremented. In this case, use You may also use any of MySQL's functions as values. Be aware that when using MySQL functions, the value will be used as it is, without being escaped! While this is ok when using a function without any arguments like Default is an empty array |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns TRUE on success of FALSE on error |
deprecated: | Deprecated starting with 2.9.12, will be removed in 3.0 |
since: | 2.1 |
void language ( string $language )
Sets the language to be used for the messages in the debugging console and in the log files
string | $language | The name of the language file from the Must be specified without the extension! (i.e. Default is "english" |
since: | 1.0.6 |
void optimize ( [ string $table = '' ] )
Optimizes all tables that have overhead (unused, lost space) in a database
string | $table | (Optional) Table to optimize May also be specified like
If not specified, all the tables in the currently selected database will be optimized. This option was added in 2.9.5 |
since: | 1.1.2 |
boolean|void option ( mixed $option , [ mixed $value = '' ] )
Sets one or more options that affect the behavior of a connection
See the options that can be set.
Usually, this method must to be called before connecting to a MySQL server because the library uses "lazy connection" and it will not actually connect to the MySQL server until the first query is run, unless the connect() method is called with the connect
argument set to TRUE
. As such, you may also call this method after the connect() method but only if no queries were run before calling this method.
This method may be called multiple times to set several options.
mixed | $option | One of the valid values described here, or an array of key/value pairs where the keys are valid values described in the previous link. |
mixed | $value | (Optional) When setting a single option this is the value to be associated with that option. When setting an array of options this argument is ignored. |
return: | Will return FALSE if there is no connection and nothing otherwise. |
since: | 2.9.5 |
boolean parse_file ( string $path )
Parses a MySQL dump file (like an export from phpMyAdmin)
If you must parse a very large file and your script crashed due to timeout or because of memory limitations, try the following:
string | $path | Path to the file to be parsed |
return: | Returns TRUE on success or FALSE on failure |
mixed query ( string $sql , [ mixed $replacements = '' ] , [ mixed $cache = false ] , [ boolean $calc_rows = false ] , [ boolean $highlight = false ] )
Runs a MySQL query
After a SELECT
query you can get the number of returned rows by reading the returned_rows property.
After an UPDATE
, INSERT
or DELETE
query you can get the number of affected rows by reading the affected_rows property.
Note that you don't necessarily need to store the result of this method in a variable for using it with a fetch method like fetch_assoc() or fetch_obj(), as all these methods, when called without the resource
argument, work on the LAST returned result resource!
string | $sql | MySQL statement to execute |
mixed | $replacements | (Optional) An array with as many items as the total parameter markers ( Default is |
mixed | $cache | (Optional) Instructs the library on whether it should cache the query results or not. Can be either The caching method is specified through the caching_method property. For unbuffered queries this argument is always Default is |
boolean | $calc_rows | (Optional) If query is a This is very useful for creating pagination or computing averages. Also, note that this information will be available without running an extra query. Here's how For unbuffered queries the value of this property will be available only after iterating over all the records with either fetch_assoc() or fetch_obj() methods. Until then, the value will be 0! Default is This argument is deprecated since 2.10.0 and will be removed in 3.0 because the technique used for it was deprecated in MySQL 8.0.17 and is pending removal in a future version. Please use the solution provided in the MySQL documentation instead. |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns a resource or an array (if results are taken from the cache) upon success, or If query results are taken from cache, the returned result will be a |
mixed query_unbuffered ()
Runs a MySQL unbuffered query.
The method's arguments are the same as for the query() method.
For unbuffered queries the values returned by returned_rows and found_rows properties will be available only after iterating over all the records with either fetch_assoc() or fetch_obj() methods, and will be 0 until then!
Until you iterate over the entire resultset, all subsequent queries will return a Commands out of sync
error unless the free_result() method is called
return: | Returns a resource or an array (if results are taken from the cache) upon success, or If query results are taken from cache, the returned result will be a |
since: | 2.9.4 |
boolean seek ( integer $row , [ resource|string $resource = '' ] )
Moves the internal row pointer of the MySQL result associated with the specified result identifier to the specified row number
The next call to a fetch method like fetch_assoc() or fetch_obj() would return that row.
integer | $row | The row you want to move the pointer to $row starts at $row should be a value in the range from |
resource|string | $resource | (Optional) Resource to fetch If not specified, the resource returned by the last run query is used |
return: | Returns TRUE on success or FALSE on failure |
since: | 1.1.0 |
mixed select ( mixed $columns , string $table , [ string $where = '' ] , [ mixed $replacements = '' ] , [ string $order = '' ] , [ mixed $limit = '' ] , [ mixed $cache = false ] , [ boolean $calc_rows = false ] , [ boolean $highlight = false ] )
Shorthand for simple SELECT
queries
For complex queries (using UNION
, JOIN
, etc) use the query() method.
When using this method column names will be enclosed in grave accents `
(thus allowing the usage of reserved words) and will be automatically escaped in order to prevent SQL injections.
mixed | $columns | A string with comma separated values or an array representing valid column names as used in a If given as a string it will be enclosed in grave accents, so make sure you are only using column names and not things like "tablename.*" or MySQL functions! You may also use |
string | $table | Table in which to search May also be specified like Note that table name (and database name, if provided) will be enclosed in grave accents |
string | $where | (Optional) A MySQL Default is |
mixed | $replacements | (Optional) An array with as many items as the total parameter markers ( Default is |
string | $order | (Optional) A MySQL Default is |
mixed | $limit | (Optional) A MySQL Default is |
mixed | $cache | (Optional) Instructs the library on whether it should cache the query results or not. Can be either The caching method is specified through the caching_method property. Default is |
boolean | $calc_rows | (Optional) If this argument is set to This is very useful for creating pagination or computing averages. Also, note that this information will be available without running an extra query. Here's how Default is This argument is deprecated since 2.10.0 and will be removed in 3.0 because the technique used for it was deprecated in MySQL 8.0.17 and is pending removal in a future version. Please use the solution provided in the MySQL documentation instead. |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns a resource or an array (if results are taken from the cache) upon success, or If query results are taken from cache, the returned result will be a |
since: | 2.0 |
boolean select_database ( string $database )
Selects the default database for queries
string | $database | Name of the database to select as the default database for queries |
return: | Returns TRUE on success or FALSE on failure |
since: | 2.9.4 |
void set_charset ( [ string $charset = 'utf8' ] , [ string $collation = 'utf8_general_ci' ] )
Sets MySQL character set and collation
The ensure that data is both properly saved and retrieved from the database you should call this method first thing after connecting to the database.
If this method is not called a warning message will be displayed in the debugging console.
Warnings can be disabled by setting the disable_warnings property.
Calling this method will not create a connection to the database but will be deferred instead until a "real" query is run. This is to ensure that the lazy connection
feature of the library is preserved.
string | $charset | (Optional) The character set to be used by the database Default is See the list of possible values |
string | $collation | (Optional) The collation to be used by the database Default is See the list of possible values |
since: | 2.0 |
boolean table_exists ( string $table )
Checks whether a table exists in a database
string | $table | The name of the table to check if it exists in the database. May also be specified like |
return: | Returns TRUE if table given as argument exists in the database, or FALSE if it does not |
since: | 2.3 |
boolean transaction_complete ()
Ends a transaction
This means that if all the queries since transaction_start() are valid, it updates the data in the database, but if any of the queries failed, it ignores all queries and treats them as if they never happened.
return: | Returns TRUE on success or FALSE on error |
since: | 2.1 |
boolean transaction_start ( [ boolean $test_only = false ] )
Starts the transaction system.
Transactions work only with databases that support transaction-safe table types. In MySQL, these are InnoDB
or BDB
table types. Working with MyISAM
tables will not raise any errors but statements will be executed automatically as soon as they are called (just like if there was no transaction).
If you are not familiar with transactions, have a look here and try to find a good online resource for more specific information.
boolean | $test_only | (Optional) Starts the transaction system in "test mode" causing the queries to be rolled back (when transaction_complete() is called) - even if all queries are valid Default is |
return: | Returns TRUE on success or FALSE on error |
since: | 2.1 |
boolean truncate ( string $table , [ boolean $highlight = false ] )
Shorthand for TRUNCATE
queries
Truncating a table is quicker then deleting all rows, as stated in the MySQL documentation. Truncating a table also resets the value of the AUTO INCREMENT column.
string | $table | Table to truncate May also be specified like |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns TRUE on success of FALSE on error |
since: | 1.0.9 |
boolean update ( string $table , mixed $columns , [ string $where = '' ] , [ mixed $replacements = '' ] , [ boolean $highlight = false ] )
Shorthand for UPDATE
queries
When using this method column names will be enclosed in grave accents `
(thus allowing the usage of reserved words) and will be automatically escaped in order to prevent SQL injections.
After an update check affected_rows to find out how many rows were affected.
string | $table | Table in which to update May also be specified like |
mixed | $columns | An associative array where the array's keys represent the columns names and the array's values represent the values to be inserted in each respective column. Column names will be enclosed in grave accents A special value may also be used for when a column's value needs to be incremented or decremented. In this case, use
...is equivalent to
You may also use any of MySQL's functions as values. Be aware that when using MySQL functions, the value will be used as it is, without being escaped! While this is ok when using a function without any arguments like |
string | $where | (Optional) A MySQL Default is |
mixed | $replacements | (Optional) An array with as many items as the total parameter markers ( Default is |
boolean | $highlight | (Optional) If set to Default is |
return: | Returns TRUE on success of FALSE on error |
since: | 1.0.9 |