spoom-php / sql-mysql
Spoom 框架的 MySQL 支持
dev-development
2018-09-02 10:14 UTC
Requires
- php: ^7.2.0
- ext-mysqli: *
- spoom-php/composer: ^1.0.0
- spoom-php/core: @dev
- spoom-php/sql: @dev
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-09-12 09:47:08 UTC
README
Spoom 是一系列协作库(扩展),您可以使用它“构建”一个符合您需求的框架。
关于 MySQL
...
安装
使用以下命令安装最新版本:
$ composer require spoom-php/sql-mysql
使用方法
您可以使用以下方式创建连接和执行命令:
<?php require __DIR__ . '/vendor/autoload.php'; use Spoom\Sql\MySQL; // TODO ...you should create an application first... // create a (lazy) connection. You can force to connect with $connection->connect() $connection = new MySQL\Connection( '127.0.0.1:3306', 'root', '', 'test_database' ); // select some items from the table 'test', where foo is 'bar' $result = $connection->execute( 'SELECT title FROM test WHERE foo = {foo}', [ 'foo' => 'bar' ] ); // the statment can be created in builder style. The code below is equivalent with the above ->execute() but it's universal // $result = $connection->statement()->addTable( 'test' )->addField( 'title' )->addFilter( 'foo = {foo}' )->search( [ 'foo' => 'bar' ] ); // results used in a loop is equals with $result->getArrayList() foreach( $result as $i => $item ) { echo "{$i}. item's title is '{$item['title']}'\n"; }
您还可以从扩展公共目录中的配置文件创建连接。文件 spoom/spoom-sql-mysql/configuration/connection.json
应包含类似以下内容:
{ "myfancyconnection": { "host": "127.0.0.1:3306", "user": "root", "password": "", "database": "test_database", "option": {} } }
然后您就可以进行以下操作:
<?php use Spoom\Sql\MySQL; // note that every instance from the same configuration name will be the exact same object $connection = MySQL\Connection::instance( 'myfancyconnection' );
许可证
Spoom 框架是开源软件,采用MIT 许可证授权。