mrcrmn/mysql

一个易于使用的MySQL包装器

0.1.1 2018-02-25 14:01 UTC

This package is auto-updated.

Last update: 2024-09-08 07:23:11 UTC


README

一个用于轻松直观地进行MySQL数据库交互的PDO包装器。

安装

要安装,请运行 composer require mrcrmn/mysql
要使用,请创建一个新的Database对象并将其连接到MySQL数据库。

$db = new Mrcrmn\Mysql\Database;
$db->connect($host, $user, $password, $port, $dbname);

用法

此库支持与数据库交互的4个基本操作:"INSERT","SELECT","UPDATE","DELETE"。

INSERT

$db->table('my_table')->insert([
   'foo' => 'bar',
   'baz' => 'foo'
]);

SELECT

示例

$db->select('firstname as fn', 'lastname as ln')->from('customers')->where('lastname', 'smith')->get();

这将返回一个关联数组作为查询结果。

可用方法
$db->select(); // default = *
$db->select(['column_1', 'column_2']); // You may also just add as many arguments as you like without the array.
$db->into('table_name'); // Sets the table name.
$db->where('column_name', 'operator', 'value'); // If you don't pass the operator it defaults to '='.
$db->orWhere('column_name', 'operator', 'value'); // Same as a where, but with the OR before it.
$db->whereIn('column_name', ['value_1', 'value_2']); // Adds a where in subquery.
$db->orWhereIn('column_name', ['value_1', 'value_2']); // Take a guess.
$db->join('table_name', 'foreign_column_name', 'local_column_name'); // the local column name defaults to 'id'
$db->leftJoin('table_name', 'foreign_column_name', 'local_column_name');
$db->rightJoin('table_name', 'foreign_column_name', 'local_column_name');
$db->orderBy('column_name', 'ASC');
$db->groupBy('column_name');
$db->limit(1);
$db->offset(8);
$db->get(); // Executes the query and returns the result as an array.
$db->first(); // Gets the first entry.
$db->count(); // Gets the number of rows in the result.
$db->getQuery(); // Returns the built query as a string.

UPDATE

$db
    ->table('my_table')
    ->where('foo', 'baz')
    ->update([
       'foo' => 'bar',
       'baz' => 'foo'
    ]);

DELETE

$db
    ->table('my_table')
    ->where('foo', 'baz')
    ->delete();