amadiify / querydb
本软件包为PHP中与mysql、sqlite和pgsql数据库系统工作提供了更多灵活性。
0.1
2019-12-01 13:11 UTC
Requires
- php: ^7.1
Requires (Dev)
- php: ^7.1
This package is not auto-updated.
Last update: 2024-10-01 15:14:32 UTC
README
- 本软件包为PHP中与mysql、sqlite和pgsql数据库系统工作提供了更多灵活性。它是Moorexa查询构建器的扩展。您应该检查一下Moorexa,以用于您下一个令人惊叹的Web应用。
如何使用
- 在 Amadiify/Connection.php 中配置数据库连接设置。您可以在运行时创建多个连接设置并切换连接。但首先,必须有一个默认连接用于回退。
以下是连接文件的外观。
'default' => [ 'dsn' => '{driver}:host={host};dbname={dbname};charset={charset}', 'driver' => 'mysql', // mysql, pgsql, sqlite 'host' => '', 'user' => '', 'password' => '', 'dbname' => '', 'charset' => 'UTF8', 'port' => '', 'handler' => 'pdo', // pdo or mysqli 'attributes'=> true, 'production'=> [ 'driver' => 'mysql', 'host' => '', 'user' => '', 'password' => '', 'dbname' => '', ], 'options' => [ PDO::ATTR_PERSISTENT => true ] ] // you can add more
安全
- 是的,它是安全的。所有查询都是预处理的,甚至包括原始SQL语句。
一旦完成,您就可以 使用 此连接。请看示例
use Amadiify\Client;
获取请求
- 执行基本获取请求(选择查询)
// generic option Client::table('user')->get(); // or Client::user()->get(); // or \user::get(); // some other configuration must be made for this to work.
更高级的获取请求
- 这超越了基础。
// using generic Client::table('user')->get('userid=?', 1); // or Client::table('user')->get('userid=?')->bind(1); // or Client::table('user')->get('username,password')->where('userid=?')->bind(1); // we can even perform two actions at the same time Client::table('user')->get('userid=?')->bind(1)->update(['username' => 'frank']); // get random Client::table('user')->get()->rand(); // using limit Client::table('user')->get()->limit(0,20); // using order Client::table('user')->get()->orderby('username', 'asc')->limit(0,20); // and much more. // see the cheat sheet for more possibilities.
插入请求
- 您可以在多个中插入。系统会保护您免受重复记录的影响。保持一切唯一存储。
- 您可以在一行中应用循环并链式调用其他操作。
// lets insert something simple $table = Client::table('user'); // simple first $table->insert(['username' => 'chris']); // multiple $table->insert(['username' => 'mack'], ['username' => 'frank']); // and much more // if you drop the line you need to instruct execution $table->insert( ['username' => 'mack'], ['username' => 'sam'] )->go(); // or $table->insert('username,password')->bind('mack', '1234'); // or $table->insert('username,password', 'mack', '1234'); // or run a get after $table->insert('username,password')->bind('mack', '1234')->get(); // returns records. $data = [ [ 'username' => 'Moorexa', 'password' => 'hash-password' ], [ 'username' => 'Wekiwork', 'password' => 'hash-password' ] ]; array_map(function($data){ Client::table('user')->insert($data); }, $data); // insert json data $table->insert('{"username":"mike2", "password":"hash-password2"}'); // or insert an object $object = (object) $data; $table->insert($object); // and much more..
更新请求
- 您可以轻松运行更新,并在此请求旁边链式调用其他操作
// get table $table = Client::table('user'); // update with json $table->update('{"username":"chris", "id":3}', 'userid=?', 3); // or $table->update('{"username":"chris", "id":3}')->where('userid=?', 3); // or $table->update('{"username":"chris", "id":3}')->where('userid=?', 3); // or $table->update(['username' => 'chris'], 'userid = ?', 3); // or $table->update(['username' => 'moorexa'])->where('userid = ?')->bind(3); // or $get = $table->get('username = ?')->bind('chris'); // then update that row $get->update(['telephone' => '080000000000']); // and much more
删除请求
- 您可以轻松运行删除查询,并在此请求旁边链式调用其他操作
// get table $table = Client::table('user'); // update with json $table->delete('userid = ?')->bind(30); // or $table->delete(['userid' => 3]); // or $get = $table->get('userid = ?', 2); // then delete that row $get->pop(); // and much more
原始SQL
- 原始SQL语句也是预处理的,让我们看一个示例
Client::sql('select * from users where username = ?', 'chris'); // or Client::sql('select * from users where username = :name')->bind('chris'); // or Client::sql('select * from users where username = "chris"'); // interpreted as // select * from users where username = :username // or $table = new Client(); $table->sql('Your query here.');
切换数据库连接
- 您可以在运行时切换到不同的连接。这很简单
$default = Client::serve(); $switch1 = Client::apply('database1'); // and other queries can be chained here
这是一个评估副本,您可以在Moorexa中使用更多功能。您可以分享并贡献,谢谢。