Download the PHP package voku/simple-mysqli without Composer
On this page you can find all versions of the php package voku/simple-mysqli. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package simple-mysqli
:gem: Simple MySQLi Class
This is a simple MySQL Abstraction Layer compatible with PHP 7+ & PHP 8.0 that provides a simple and secure interaction with your database using mysqli_* functions at its core. This is perfect for small scale applications such as cron jobs, facebook canvas campaigns or micro frameworks or sites.
You can also use the :ring: "Simple Active Record"-class, it's based on this db class and add some OOP syntax. But please inform you about "Active Record" vs "Data Mapper" before you use it.
Get "Simple MySQLi"
You can download it from here, or require it using composer.
Install via "composer require"
- Starting the driver
- Multiton && Singleton
- Doctrine/DBAL as parent driver
- Using the "DB"-Class
- Selecting and retrieving data from a table
- Inserting data on a table
- Binding parameters on queries
- Transactions
- Using the "Result"-Class
- Fetching all data
- Fetching database-table-fields
- Fetching + Callable
- Fetching + Transpose
- Fetching + Pairs
- Fetching + Groups
- Fetching + first
- Fetching + last
- Fetching + slice
- Fetching + map
- Fetching + aliases
- Fetching + Iterations
- Using the "Prepare"-Class
- INSERT-Prepare-Query (example)
- SELECT-Prepare-Query (example)
- Logging and Errors
- Changelog
Starting the driver
Multiton && Singleton
You can use without any parameters and you will get your (as "singleton") first initialized connection. Or you can change the parameter and you will create an new "multiton"-instance which works like an singleton, but you need to use the same parameters again, otherwise (without the same parameter) you will get an new instance.
Doctrine/DBAL as parent driver
Using the "DB"-Class
There are numerous ways of using this library, here are some examples of the most common methods.
Selecting and retrieving data from a table
But you can also use a method for select-queries:
Example: SELECT
Here is a list of connectors for the "WHERE"-array: 'NOT', 'IS', 'IS NOT', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN', 'LIKE', 'NOT LIKE', '>', '<', '>=', '<=', '<>', '+', '-'
INFO: use an array as $value for "[NOT] IN" and "[NOT] BETWEEN"
INFO: use + / - in the value not in the key of the $data
Example: UPDATE with "page_template = page_template + 1"
Example: SELECT with "NOT IN"
Example: SELECT with Cache
The result (via $result->fetchAllArray()) is only cached for 3600s when the query was a SELECT statement, otherwise you get the default result from the function.
Inserting data on a table
to manipulate tables you have the most important methods wrapped, they all work the same way: parsing arrays of key/value pairs and forming a safe query
the methods are:
All methods will return the resulting mysqli_insert_id()
or true/false depending on context.
The correct approach if to always check if they executed as success is always returned
Example: DELETE
note: all parameter values are sanitized before execution, you don\'t have to escape values beforehand.
Example: INSERT
Example: REPLACE
Binding parameters on queries
Binding parameters is a good way of preventing mysql injections as the parameters are sanitized before execution.
Transactions
Use begin()
, commit()
, and rollback()
to manage transactions:
Any SQL errors between begin()
and commit()
will yield a RuntimeException
.
You can also use the DB->transact()
method. The following is equivalent
to the above:
Using the "Result"-Class
After executing a SELECT
query you receive a Result
object that will help you manipulate the resultant data.
there are different ways of accessing this data, check the examples bellow:
Fetching all data
Fetching all data works as Result::RESULTTYPE* the fetchAll()
and fetch()
method will return the default based on the $_default_result_type
config.
Other methods are:
Fetching database-table-fields
Returns rows of field information in a result set:
Pass true
as argument if you want each field information returned as an
associative array instead of an object. The default is to return each as an
object, exactly like the mysqli_fetch_fields
function.
Fetching + Callable
Fetches a row or a single column within a row:
This method forms the basis of all fetch methods. All forms of fetch advances
the internal row pointer to the next row. null
will be returned when there are
no more rows to be fetched.
Fetching + Transpose
Returns all rows at once, transposed as an array of arrays:
Transposing a result set of X rows each with Y columns will result in an array of Y rows each with X columns.
Pass a column name as argument to return each column as an associative array with keys taken from values of the provided column. If not provided, the keys will be numeric starting from zero.
e.g.:
Fetching + Pairs
Returns all rows at once as key-value pairs using the column in the first argument as the key:
Pass a column name as the second argument to only return a single column as the value in each pair:
Fetching + Groups
Returns all rows at once as a grouped array:
Pass a column name as the second argument to only return single columns as the values in each groups:
Fetching + first
Returns the first row element from the result:
Pass a column name as argument to return a single column from the first row:
Fetching + last
Returns the last row element from the result:
Pass a column name as argument to return a single column from the last row:
Fetching + slice
Returns a slice of rows from the result:
The above will return 10 rows skipping the first one. The first parameter is the
zero-based offset; the second parameter is the number of elements; the third
parameter is a boolean value to indicate whether to preserve the keys or not
(optional and defaults to false). This methods essentially behaves the same as
PHP's built-in array_slice()
function.
Fetching + map
Sets a mapper callback function that's used inside the Result->fetchCallable()
method:
The above example will map one row (0) from the result into a object. Set the mapper callback function to null to disable it.
Fetching + aliases
Fetching + Iterations
To iterate a result-set you can use any fetch() method listed above.
Executing Multi Queries
To execute multiple queries you can use the method. You can use multiple queries separated by "".
Return-Types:
- "Result"-Array by "SELECT"-queries
- "bool" by only "INSERT"-queries
- "bool" by only (affected_rows) by "UPDATE / DELETE"-queries
- "bool" by only by e.g. "DROP"-queries
e.g.:
Using the "Prepare"-Class
Prepare statements have the advantage that they are built together in the MySQL-Server, so the performance is better.
But the debugging is harder and logging is impossible (via PHP), so we added a wrapper for "bind_param" called "bind_param_debug". With this wrapper we pre-build the sql-query via php (only for debugging / logging). Now you can e.g. echo the query.
INFO: You can still use "bind_param" instead of "bind_param_debug", e.g. if you need better performance.
INSERT-Prepare-Query (example)
SELECT-Prepare-Query (example)
Logging and Errors
You can hook into the "DB"-Class, so you can use your personal "Logger"-Class. But you have to cover the methods:
You can also disable the logging of every sql-query, with the "getInstance()"-parameter "logger_level" from "DB"-Class. If you set "logger_level" to something other than "TRACE" or "DEBUG", the "DB"-Class will log only errors anymore.
Showing the query log: The log comes with the SQL executed, the execution time and the result row count.
To debug mysql errors, use $db->errors()
to fetch all errors (returns false if there are no errors) or $db->lastError()
for information about the last error.
But the easiest way for debugging is to configure "DB"-Class via "DB::getInstance()" to show errors and exit on error (see the example above). Now you can see SQL-errors in your browser if you are working on "localhost" or you can implement your own "checkForDev()" via a simple function, you don't need to extend the "Debug"-Class. If you will receive error-messages via e-mail, you can implement your own "mailToAdmin()"-function instead of extending the "Debug"-Class.
Changelog
See CHANGELOG.md.
Support
For support and donations please visit Github | Issues | PayPal | Patreon.
For status updates and release announcements please visit Releases | Twitter | Patreon.
For professional support please contact me.
Thanks
- Thanks to GitHub (Microsoft) for hosting the code and a good infrastructure including Issues-Managment, etc.
- Thanks to IntelliJ as they make the best IDEs for PHP and they gave me an open source license for PhpStorm!
- Thanks to Travis CI for being the most awesome, easiest continous integration tool out there!
- Thanks to StyleCI for the simple but powerfull code style check.
- Thanks to PHPStan && Psalm for relly great Static analysis tools and for discover bugs in the code!
License
All versions of simple-mysqli with dependencies
ext-mysqli Version *
voku/arrayy Version ~6.0 || ~7.0
voku/simple-cache Version ~4.0
voku/portable-utf8 Version ~6.0
voku/phonetic-algorithms Version ~5.0
symfony/property-access Version ~2.8 || ~3.3 || ~4.0 || ~5.0 || ~6.0