https://github.com/slamdunk/mysql-php
PHP version of mysql cli that comes with MySQL
https://github.com/slamdunk/mysql-php
mysql php
Last synced: 8 months ago
JSON representation
PHP version of mysql cli that comes with MySQL
- Host: GitHub
- URL: https://github.com/slamdunk/mysql-php
- Owner: Slamdunk
- License: mit
- Created: 2019-07-17T09:32:39.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2024-09-13T06:12:06.000Z (almost 2 years ago)
- Last Synced: 2024-09-13T17:22:47.287Z (almost 2 years ago)
- Topics: mysql, php
- Language: PHP
- Size: 123 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# MySQL bin in PHP
[](https://packagist.org/packages/slam/mysql-php)
[](https://packagist.org/packages/slam/mysql-php)
[](https://github.com/Slamdunk/mysql-php/actions)
[](https://codecov.io/gh/Slamdunk/mysql-php?branch=master)
PHP light version of mysql cli that comes with MySQL.
## Why
1. You are inside a PHP only environment, like a PHP Docker image
1. You need to import a large mysql dump
1. You don't have access to the native `mysql` client
## Performance
Speed is exactly the **same** of the original `mysql` binary thanks to streams usage.
## Supported formats
|Input type|Example|Supported?|
|---|---|:---:|
|`mysqldump` output|*as is*|:heavy_check_mark:|
|Single query on single line|`SELECT NOW();`|:heavy_check_mark:|
|Single query on multiple lines|`SELECT`
`NOW();`|:heavy_check_mark:|
|Multiple queries on separated single or multiple lines|`SELECT NOW();`
`SELECT`
`NOW();`|:heavy_check_mark:|
|Multiple queries on single line|`SELECT NOW();SELECT NOW();`|:x:|
## Usage
The library provides two usages, the binary and the `\SlamMysql\Mysql` class.
### From CLI
```
$ ./mysql -h
Usage: mysql [OPTIONS]
--host Connect to host [Default: INI mysqli.default_host]
--port Port number [Default: INI mysqli.default_port]
--username User for login [Default: INI mysqli.default_user]
--password Password to use [Default: INI mysqli.default_pw]
--database Database to use [Default: empty]
--socket The socket file [Default: INI mysqli.default_socket]
$ printf "CREATE DATABASE foobar;\nSHOW DATABASES;" | ./mysql
information_schema
foobar
mysql
performance_schema
sys
$ ./mysql --database foobar < foobar_huge_dump.sql
```
### From PHP
```php
$mysql = new \SlamMysql\Mysql('localhost', 'root', 'pwd', 'my_database', 3306, '/socket');
$return = $mysql->run(\STDIN, \STDOUT, \STDERR);
exit((int) (true !== $return));
```
`\SlamMysql\Mysql::run` accepts any type of resource consumable by `fgets/fwrite` functions.
## Related projects
1. [ifsnop/mysqldump-php](https://github.com/ifsnop/mysqldump-php): `mysqldump` binary port in pure PHP