Download the PHP package amcsi/amysql without Composer
On this page you can find all versions of the php package amcsi/amysql. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package amysql
Short Description A MySQL(i) wrapper for PHP 5.2 and higher with useful tools and no extension dependencies.
License MIT
Homepage https://github.com/amcsi/amysql
Informations about the package amysql
AMysql
A MySQL wrapper guaranteed to work on any PHP 5.2+ or HHVM configuration with the mysqli extension or mysql_*
functions available.
Also contains many tools to help build queries, manage them, profile them.
Is AMysql for you?
- Feel like Zend_Db is way too bulky, but on the other hand does not give you useful tools for common problems?
- Afraid of using PDO or Mysqli as it might not be available on the production server?
- Do not like the boring task of assembling INSERTs and UPDATEs?
Then AMysql is the library for your project!
What AMysql tries to be
- A library that allows you to work with mysql with any PHP configuration without requiring any extensions.
- A practical library that provides great tools for common situations in MySQL. This includes tools not available in any MySQL API (extension level or PHP level), or the combination of tools that would otherwise only be available separately in several different APIs, but never together.
What AMysql is not
- It's not an abstraction library. Although I might add PostgreSQL support, the main focus is on MySQL, as abstraction takes focus away from being able to make good tools specifically for a type of database. Also, it would be too much work for me alone to look into how all the other databases function.
- It's not an ORM. There is support for fetching data into new instances of classes, but the focus is on ease of building queries.
Requirements
PHP 5.1.0+ is required, although this has only been tested on 5.2 and higher, and either the MySQLi extension or the mysql_* functions must be available.
Installation
Available on packagist.
See INSTALL file.
Usage
Typically you want to make one instance of AMysql per db connection. AMysql lazy connects by default.
Apigen docs are available for this project: http://amcsi.github.io/amysql/apigen/
Instantiating AMysql
When instantiating AMysql, you can pass in either a mysql link resource or connection details as an array.
or
or
The full connection details array supports:
host
Host to connect tousername
Mysql user usernamepassword
Mysql user passworddb
The database to selectport
clientFlags
mysql_* only. For themysql_connect()
$client_flags
argumentdriver
mysql
ormysqli
. Prefer a specific driver. By default when making a new connection, AMysql tries to useMySQLi
if it is available, otherwise it falls back to themysql_*
functionssocket
- socketdefaultAutoCommit
Specify here whether autocommit is on. When using mysqli and autocommit is off on the MySQL server, this should be set to false.autoPingSeconds
Each time a query is executed, it is checked if the given amount of seconds has passed since the last query, and if so, pings the server and reconnects if the connection has been lost. It is recommended that this is set to 20 seconds and prevents the script from dying from a 2006 error. Off (NULL) by default.autoReconnect
If this is TRUE and an executed query fails with a 2006 mysql error, try to reconnect with the connection details and attempt to reexecute the query once before giving up. It is recommended that you set this to TRUE. WARNING: there might be edge cases related to writing data and the max packets setting which may result in 2006 mysql errors even if though the INSERT/UPDATE want through, resulting in a double write. I don't quite understand this, but it's never happened to me before. If you want to be extra careful, don't set this to TRUE, and only rely onautoPingSeconds
instead.
Inserting mysql expressions
Within prepared statement you can purposefully bind literal strings that will not be escaped or enclosed by quotes. Use it with caution.
To use it, see the example below:
AMysql_Expr also supports a few predefined special expressions not only consisting of literal values, such as for making an IN
list or for doing proper escaping for a LIKE
expression. For more information, check out the AMysql/Expr.php file.
Example for an IN
list:
Example for escaping LIKE. Note the automatic escaping handling with the = sign ( http://stackoverflow.com/a/3683868/1381550 ). Do not worry about actual percent and underscore lines, as they will properly be escaped and will be treated as literal characters. The default LIKE pattern is wrapping your search string between two (%) signs for a "contains" match. This is represented internally with a sprintf()
pattern of %%%s%s
meaning (literal %), (the string), (literal %). You can change this in the 3rd parameter of AMysql_Expr::__construct
Inserting multiple rows of data
Updating a single row
Updating multiple rows
You can update multiple rows with the same insert()
method as for single rows if you pass a multidimensional array. It can be an array or rows, or an array of columns with an array of values.
Deleting rows
Queries throw AMysql_Exceptions by default.
AMysql_Exceptions can be checked for several constraint related rejections in a fairly simple and not too error prone way.
Selecting (without AMysql_Select)
Note that if there is only 1 question mark in the prepared string, you may also pass the $binds
as a scalar value and it will be treated as if it were the within an array. If you are expecting a possible null
value to be bound, do not use the scalar method though (always use an array in that case).
P.S. this is also true for every method that expects a $binds
array.
Preparing select first, executing later (without AMysql_Select)
And now with a new AMysql_Select class to help assemble a SELECT SQL string:
Read the commented AMysql_Select file for more details.
A documentation on binding parameters can be found in the comments for AMysql_Statement::execute(). Be sure to check it out.
Profiling
A simple example without having to make your own HTML template:
Alternatively, check out AMysql_Profiler for more options.
Other methods
Many other useful methods are available as well. Check out the source files and read the documentation for the methods.
Apigen docs are available for this project: http://amcsi.github.io/amysql/apigen/
License
MIT
Contribution
If anything doesn't work the way you expect it to, please report in as an issue on github.
If you can provide pull requests to fixes, that would be even better. Work from the master
or develop
branch, whichever is the latest.