Download the PHP package react/mysql without Composer
On this page you can find all versions of the php package react/mysql. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mysql
MySQL
Async MySQL database client for ReactPHP.
Development version: This branch contains the code for the upcoming version 0.7 release. For the code of the current stable version 0.6 release, check out the
0.6.x
branch.The upcoming version 0.7 release will be the way forward for this package. However, we will still actively support version 0.6 for those not yet on the latest version. See also installation instructions for more details.
This is a MySQL database driver for ReactPHP. It implements the MySQL protocol and allows you to access your existing MySQL database. It is written in pure PHP and does not require any extensions.
Table of contents
- Quickstart example
- Usage
- MysqlClient
- __construct()
- query()
- queryStream()
- ping()
- quit()
- close()
- error event
- close event
- Install
- Tests
- License
Quickstart example
This example runs a simple SELECT
query and dumps all the records from a book
table:
See also the examples.
Usage
MysqlClient
The MysqlClient
is responsible for exchanging messages with your MySQL server
and keeps track of pending queries.
This class represents a connection that is responsible for communicating with your MySQL server instance, managing the connection state and sending your database queries. Internally, it creates the underlying database connection only on demand once the first request is invoked on this instance and will queue all outstanding requests until the underlying connection is ready. This underlying connection will be reused for all requests until it is closed. By default, idle connections will be held open for 1ms (0.001s) when not used. The next request will either reuse the existing connection or will automatically create a new underlying connection if this idle time is expired.
From a consumer side this means that you can start sending queries to the database right away while the underlying connection may still be outstanding. Because creating this underlying connection may take some time, it will enqueue all outstanding commands and will ensure that all commands will be executed in correct order once the connection is ready.
If the underlying database connection fails, it will reject all outstanding commands and will return to the initial "idle" state. This means that you can keep sending additional commands at a later time which will again try to open a new underlying connection. Note that this may require special care if you're using transactions that are kept open for longer than the idle period.
Note that creating the underlying connection will be deferred until the
first request is invoked. Accordingly, any eventual connection issues
will be detected once this instance is first used. You can use the
quit()
method to ensure that the connection will be soft-closed
and no further commands can be enqueued. Similarly, calling quit()
on
this instance when not currently connected will succeed immediately and
will not have to wait for an actual underlying connection.
__construct()
The new MysqlClient(string $uri, ConnectorInterface $connector = null, LoopInterface $loop = null)
constructor can be used to
create a new MysqlClient
instance.
The $uri
parameter must contain the database host, optional
authentication, port and database to connect to:
Note that both the username and password must be URL-encoded (percent-encoded) if they contain special characters:
You can omit the port if you're connecting to default port 3306
:
If you do not include authentication and/or database, then this method
will default to trying to connect as user root
with an empty password
and no database selected. This may be useful when initially setting up a
database, but likely to yield an authentication error in a production system:
This method respects PHP's default_socket_timeout
setting (default 60s)
as a timeout for establishing the underlying connection and waiting for
successful authentication. You can explicitly pass a custom timeout value
in seconds (or use a negative number to not apply a timeout) like this:
By default, idle connections will be held open for 1ms (0.001s) when not used. The next request will either reuse the existing connection or will automatically create a new underlying connection if this idle time is expired. This ensures you always get a "fresh" connection and as such should not be confused with a "keepalive" or "heartbeat" mechanism, as this will not actively try to probe the connection. You can explicitly pass a custom idle timeout value in seconds (or use a negative number to not apply a timeout) like this:
By default, the connection provides full UTF-8 support (using the
utf8mb4
charset encoding). This should usually not be changed for most
applications nowadays, but for legacy reasons you can change this to use
a different ASCII-compatible charset encoding like this:
If you need custom connector settings (DNS resolution, TLS parameters, timeouts,
proxy servers etc.), you can explicitly pass a custom instance of the
ConnectorInterface
:
This class takes an optional LoopInterface|null $loop
parameter that can be used to
pass the event loop instance to use for this object. You can use a null
value
here in order to use the default loop.
This value SHOULD NOT be given unless you're sure you want to explicitly use a
given event loop instance.
query()
The query(string $query, array $params = []): PromiseInterface<MysqlResult>
method can be used to
perform an async query.
This method returns a promise that will resolve with a MysqlResult
on
success or will reject with an Exception
on error. The MySQL protocol
is inherently sequential, so that all queries will be performed in order
and outstanding queries will be put into a queue to be executed once the
previous queries are completed.
If this SQL statement returns a result set (such as from a SELECT
statement), this method will buffer everything in memory until the result
set is completed and will then resolve the resulting promise. This is
the preferred method if you know your result set to not exceed a few
dozens or hundreds of rows. If the size of your result set is either
unknown or known to be too large to fit into memory, you should use the
queryStream()
method instead.
You can optionally pass an array of $params
that will be bound to the
query like this:
The given $sql
parameter MUST contain a single statement. Support
for multiple statements is disabled for security reasons because it
could allow for possible SQL injection attacks and this API is not
suited for exposing multiple possible results.
queryStream()
The queryStream(string $sql, array $params = []): ReadableStreamInterface
method can be used to
perform an async query and stream the rows of the result set.
This method returns a readable stream that will emit each row of the
result set as a data
event. It will only buffer data to complete a
single row in memory and will not store the whole result set. This allows
you to process result sets of unlimited size that would not otherwise fit
into memory. If you know your result set to not exceed a few dozens or
hundreds of rows, you may want to use the query()
method instead.
You can optionally pass an array of $params
that will be bound to the
query like this:
This method is specifically designed for queries that return a result set
(such as from a SELECT
or EXPLAIN
statement). Queries that do not
return a result set (such as a UPDATE
or INSERT
statement) will not
emit any data
events.
See also ReadableStreamInterface
for more details about how readable streams can be used in ReactPHP. For
example, you can also use its pipe()
method to forward the result set
rows to a WritableStreamInterface
like this:
Note that as per the underlying stream definition, calling pause()
and
resume()
on this stream is advisory-only, i.e. the stream MAY continue
emitting some data until the underlying network buffer is drained. Also
notice that the server side limits how long a connection is allowed to be
in a state that has outgoing data. Special care should be taken to ensure
the stream is resumed in time. This implies that using pipe()
with a
slow destination stream may cause the connection to abort after a while.
The given $sql
parameter MUST contain a single statement. Support
for multiple statements is disabled for security reasons because it
could allow for possible SQL injection attacks and this API is not
suited for exposing multiple possible results.
ping()
The ping(): PromiseInterface<void>
method can be used to
check that the connection is alive.
This method returns a promise that will resolve (with a void value) on
success or will reject with an Exception
on error. The MySQL protocol
is inherently sequential, so that all commands will be performed in order
and outstanding command will be put into a queue to be executed once the
previous queries are completed.
quit()
The quit(): PromiseInterface<void>
method can be used to
quit (soft-close) the connection.
This method returns a promise that will resolve (with a void value) on
success or will reject with an Exception
on error. The MySQL protocol
is inherently sequential, so that all commands will be performed in order
and outstanding commands will be put into a queue to be executed once the
previous commands are completed.
This method will gracefully close the connection to the MySQL database
server once all outstanding commands are completed. See also
close()
if you want to force-close the connection without
waiting for any commands to complete instead.
close()
The close(): void
method can be used to
force-close the connection.
Unlike the quit()
method, this method will immediately force-close the
connection and reject all outstanding commands.
Forcefully closing the connection will yield a warning in the server logs
and should generally only be used as a last resort. See also
quit()
as a safe alternative.
error event
The error
event will be emitted once a fatal error occurs, such as
when the connection is lost or is invalid.
The event receives a single Exception
argument for the error instance.
This event will only be triggered for fatal errors and will be followed by closing the connection. It is not to be confused with "soft" errors caused by invalid SQL queries.
close event
The close
event will be emitted once the connection closes (terminates).
See also the close()
method.
Install
The recommended way to install this library is through Composer. New to Composer?
Once released, this project will follow SemVer. At the moment, this will install the latest development version:
See also the CHANGELOG for details about version upgrades.
This project aims to run on any platform and thus does not require any PHP extensions and supports running on legacy PHP 5.4 through current PHP 8+ and HHVM. It's highly recommended to use the latest supported PHP version for this project.
Tests
To run the test suite, you first need to clone this repo and then install all dependencies through Composer:
The test suite contains a number of functional integration tests that send actual test SQL queries against your local database and thus rely on a local MySQL test database with appropriate write access. The test suite creates and modifies a test table in this database, so make sure to not use a production database! You can change your test database credentials by passing these ENV variables:
For example, to create an empty test database, you can also use a temporary
mysql
Docker image like this:
To run the test suite, go to the project root and run:
License
MIT, see LICENSE file.
This is a community project now managed by @friends-of-reactphp. The original implementation was created by @bixuehujin starting in 2013 and has been migrated to @friends-of-reactphp in 2018 to help with maintenance and upcoming feature development.
The original implementation was made possible thanks to the following projects:
- phpdaemon: the MySQL protocol implementation is based on code of this project (with permission).
- node-mysql: the API design is inspired by this project.
All versions of mysql with dependencies
evenement/evenement Version ^3.0 || ^2.1 || ^1.1
react/event-loop Version ^1.2
react/promise Version ^3 || ^2.7
react/promise-stream Version ^1.6
react/promise-timer Version ^1.9
react/socket Version ^1.12