madbyad / mpl-mysql
一个PHP库,提供与MySQL数据库通信的简单接口
Requires
- php: >= 7.0.0
Requires (Dev)
- phpunit/phpunit: ^9.6
README
MPL (MadByAd PHP Library) MySQL是一个PHP库,用于提供与MySQL数据库通信的简单接口。这个库包含一个查询执行器,用于运行您自己编写的查询(需要广泛的MySQL知识),一个查询构建器,用于轻松编写查询或CRUD(表示创建、读取、更新、删除),允许您运行MySQL的四个基本查询:INSERT
、SELECT
、UPDATE
、DELETE
(查询构建器和CRUD需要最少MySQL查询知识)
安装
要安装该包,请打开composer并写入以下命令
composer require madbyad/mpl-mysql
MySQL类
MySQL
类用于建立MySQL连接
构造类
要建立MySQL连接,可以创建一个新的MySQL(string $hostname = null, string $username = null, string $password = null, string $database = null, int $port = null)
类。它接受5个参数,分别是MySQL主机名、MySQL用户名、MySQL用户密码、要连接的数据库名和端口号。
建立默认连接
您还可以建立默认连接,这意味着每次使用MySQLQuery
、MySQLBuilder
或MySQLCRUD
类时,您不需要建立新的连接,只需使用默认连接。要建立默认连接,可以使用方法MySQL::setDefaultConnection(string $hostname = null, string $username = null, string $password = null, string $database = null, int $port = null)
。它接受5个参数,分别是MySQL主机名、MySQL用户名、MySQL用户密码、要连接的数据库名和端口号。
获取连接
如果您有一个MySQL
类的实例,您可以使用方法getConnection()
来获取MySQL连接
或者如果您已经建立了默认连接,您可以使用方法MySQL::getDefaultConnection()
示例
// establish a new connection $mysql = new MySQL("localhost", "root", "", "my_database"); // return the mysql connection $mysql->getConnection(); // establish a default connection MySQL::setDefaultConnection("localhost", "root", "", "my_database"); // return the default connection MySQL::getDefaultConnection();
MySQLQuery类
MySQLQuery
类允许您创建一个查询,将值绑定到查询,并执行查询。它简单且提供很多自由度,但使用它需要广泛的MySQL知识
构造类
在构建一个新的MySQLQuery(string $query, mysqli $connection = null)
类时,它接受1个参数和另一个可选参数,分别是查询和MySQL连接。如果未提供连接,则将使用由MySQL
类建立的默认连接,如果没有默认连接,则将抛出MySQLNoConnectionException
示例1
// prepare a new query // no connection is supplied so it will use the default connection $query = new MySQLQuery("SELECT * FROM user WHERE name LIKE ?");
示例2
// Establish a mysql connection $mysql = new MySQL("localhost", "root", "", "my_database"); // prepare a new query // use the given connection $query = new MySQLQuery("SELECT * FROM user WHERE name LIKE ?", $mysql);
绑定值
要将值绑定到查询中,您可以使用bind(...$values)
方法
为什么要绑定值,而不是直接插入查询中?这样做是为了防止SQL注入。
示例
// prepare a new query // no connection is supplied so it will use the default connection $query = new MySQLQuery("SELECT * FROM user WHERE name LIKE ?"); // bind values into the query $query->bind($name);
执行查询
最后,要执行查询,您可以使用execute()
方法。
示例
// prepare a new query // no connection is supplied so it will use the default connection $query = new MySQLQuery("SELECT * FROM user WHERE name LIKE ?"); // bind values into the query $query->bind($name); // execute the query $query->execute();
获取结果
如果查询是SELECT
查询,您可能想要获取结果。要获取结果,您可以使用result()
方法。这将返回所选行,形式为关联数组。
示例
// prepare a new query // no connection is supplied so it will use the default connection $query = new MySQLQuery("SELECT * FROM user WHERE name LIKE ?"); // bind values into the query $query->bind($name); // execute the query $query->execute(); // return the result since it is a SELECT query $result = $query->result();
MySQLCRUD类
MySQLCRUD
类允许您执行4个基本的SQL查询,即CREATE
或INSERT
、READ
或SELECT
、UPDATE
和DELETE
。使用MySQLCRUD
类需要您对MySQL有最基本的知识,并且无需编写任何SQL查询。
创建数据
要创建或插入数据,您可以使用MySQLCRUD::create(string $table, array $columns, array $values, mysqli|MySQL|null $connection = null)
方法。它接受3个参数和1个可选参数,第一个是要创建数据的表,第二个是确定应填充哪些列的列,第三个是列的值,最后一个参数是连接,如果为null
,则将使用默认连接。
示例
// create a new user MySQLCRUD::create("user", ["name", "password"], [$name, $password]);
读取数据
要读取数据,您可以使用MySQLCRUD::read(string $table, array $columns = null, array $condition = null, array $values = null, array $readSettings = null, mysqli|MySQL|null $connection = null)
方法。它接受6个参数,第一个是读取数据的表,第二个是确定要读取哪些列的列,第三个是条件,第四个是将绑定到条件的值(条件的值),第五个是读取设置,它是一个包含设置的关联数组(可以用于确定限制、偏移量和排序),最后一个参数是连接,如果为null
,则将使用默认连接。此方法将以关联数组的形式返回读取的行。
示例1
// read from the table user and read all column MySQLCRUD::read("user");
示例2
// read from the table user // and only read the name and description column MySQLCRUD::read("user", ["name", "description"]);
示例3
// read from the table user // but only read if the name and password match the given value MySQLCRUD::read("user", [], ["name = ?", "password = ?"], [$name, $password]);
示例4
// read from the table user // limit the readed rows by 10 // offset the starting rows to read by 10 MySQLCRUD::read("user", [], [], [], [ "limit" => 10, "offset" => 10, ]);
示例5
// read from the table user // order the return rows by name ascendingly (A to Z) MySQLCRUD::read("user", [], [], [], [ "orderBy" => "name", "orderType" => "ascending", ]);
示例6
// read from the table user // order the return rows by name descendingly (Z to A) MySQLCRUD::read("user", [], [], [], [ "orderBy" => "name", "orderType" => "ascending", ]);
更新数据
要更新数据,您可以使用MySQLCRUD::update(string $table, array $columns, array $columnValues, array $condition = null, array $conditionValues = null, mysqli|MySQL|null $connection = null)
方法。它接受6个参数,第一个是要更新数据的表,第二个是要更新的列列表,第三个是这些列的新值,第四个是条件要求,第五个是条件的值,第六个是MySQL连接,如果为null
,则将使用默认连接。
示例
// Update the user description who has the given name and id MySQLCRUD::update( "user", ["description = ?"], [$description], ["name = ?", "id = ?"], [$name, $id] );
删除数据
要删除数据,您可以使用MySQLCRUD::delete(string $table, array $condition = null, array $values = null, mysqli|MySQL|null $connection = null)
方法。它接受4个参数,第一个是要删除数据的表,第二个是要删除的数据的条件,第三个是条件的值,第四个是MySQL连接,如果为null
,则将使用默认连接。
示例1
// delete a user who has the given name and id MySQLCRUD::delete("user", ["name = ?", "id = ?"], [$name, $id]);
示例2
// WARNING // if no condition is supplied this will delete all data MySQLCRUD::delete("user");
MySQLBuilder类
MySQLBuilder
类允许您构建查询而不是编写MySQL查询。要使用它,需要您对MySQL查询有最基本的知识。
构造类
当构造一个新的MySQLBuilder(mysqli $connection = null)
类时,它接受1个可选参数,即MySQL连接。如果没有提供连接,则将使用由MySQL
类建立的默认连接,如果没有默认连接,则将抛出MySQLNoConnectionException
。
插入值
要将值插入到表中,与MySQLCRUD
类类似,只是它使用queryInsert(string $table, array $columns, array $values)
方法,并接受3个参数,第一个是表,第二个是要填充的列,第三个是列的值。
示例
// construct the class $query = new MySQLBuilder; // insert a new user $query->queryInsert("user", ["name", "password"], [$name, $password]);
选择值
要构建选择查询,您可以使用querySelect(string $table, array $columns = [])
方法。它接受2个参数,第一个是表,第二个是要选择的列。
示例1
// construct the class $query = new MySQLBuilder; // select from the table user and grab all column $query->querySelect("user", []);
示例2
// construct the class $query = new MySQLBuilder; // select from the table user and grab the name and description column $query->querySelect("user", ["name", "description"]);
execute()
execute()
方法将执行构建的查询。
示例
// construct the class $query = new MySQLBuilder; // * select from the table user and grab the name and description column // * then executed the query $query->querySelect("user", ["name", "description"]) ->execute();
join()
《join(string $tableToJoin, array $columns = [], string $columnFromTable = "", string $columnFromJoin = "")
》方法用于连接表格。它接受4个参数,第一个是连接的表格,第二个是从连接的表格中要选择的列,第三个是原始表中用于与连接表上的列进行比较的列,第四个是用于与原始表上的列进行比较的连接表上的列。
示例
// construct the class $query = new MySQLBuilder; // * select from the table post // * join the table user // based on the column owner on the post table // and the column name on the user table // * then executed the query $query->querySelect("post", []) ->join("user", [], "owner", "name") ->execute();
condition()
《condition(array|string $conditions, array|string|int|float $values)
》方法用于向查询添加条件。它接受2个参数,第一个是条件或条件的列表,第二个是条件对应的值/值。
示例1
// construct the class $query = new MySQLBuilder; // * select from the table post // * only select if the name and id match the given name and id // * then executed the query $query->querySelect("user", []) ->condition(["name = ?", "id = ?"], [$name, $id]) ->execute();
示例2
// construct the class $query = new MySQLBuilder; // * select from the table post // * only select if the name match the name A or name B or Name C // * then executed the query $query->querySelect("user", []) ->condition(["name = ?"], [$nameA]) ->condition(["name = ?"], [$nameB]) ->condition(["name = ?"], [$nameC]) ->execute();
示例3
// construct the class $query = new MySQLBuilder; // * select from the table post // * only select if the name match the given name or the given id // * then executed the query $query->querySelect("user", []) ->condition(["name = ?"], [$name]) ->condition(["id = ?"], [$id]) ->execute();
limit()
《limit(int $amount)
》方法用于设置选择限制。
示例
// construct the class $query = new MySQLBuilder; // * select from the table post // * only select if the name match the given name // * limit the selection by 10 // * then executed the query $query->querySelect("user", []) ->condition(["name = ?"], [$name]) ->limit(10); ->execute();
offset()
《offset(int $amount)
》方法用于设置选择偏移量。
示例1
// construct the class $query = new MySQLBuilder; // * select from the table post // * only select if the name match the given name // * offset the selection by 10 // * then executed the query $query->querySelect("user", []) ->condition(["name = ?"], [$name]) ->offset(10); ->execute();
示例2
// construct the class $query = new MySQLBuilder; // * select from the table post // * limit the selection by 10 // * offset the selection by 10 // * then executed the query $query->querySelect("user", []) ->limit(10); ->offset(10); ->execute();
orderBy()
《orderBy(string $column, bool $ascending = true)
》方法用于按特定列排序选择结果。它接受2个参数,第一个是要排序的选择列,第二个是是否应该按升序排序,如果为false
,则按降序排序。
示例1
// construct the class $query = new MySQLBuilder; // * select from the table post // * order the selection by name column ascendingly A to Z // * then executed the query $query->querySelect("user", []) ->orderBy("name") ->execute();
示例2
// construct the class $query = new MySQLBuilder; // * select from the table post // * order the selection by name column descendingly Z to A // * then executed the query $query->querySelect("user", []) ->orderBy("name", false) ->execute();
更新值
要构造更新查询,可以使用queryUpdate(string $table)
方法,它只接受1个参数,即表。
示例
// construct the class $query = new MySQLBuilder; // * update the user table $query->queryUpdate("user");
set()
《set(string $column, array|string|int|float $value)
》方法用于设置给定列的新值。它接受2个参数,第一个是要更新的列,第二个是新的值。
示例1
// construct the class $query = new MySQLBuilder; // * update the user table // * update the column name to be the given new name // * update the column picture to be the given new picture $query->queryUpdate("user") ->set("name", $name); ->set("picture", $picture);
execute()
execute()
方法将执行构建的查询。
示例
// construct the class $query = new MySQLBuilder; // * update the user table // * update the column name to be the given new name // * update the column picture to be the given new picture // * then executed the query $query->querySelect("user") ->set("name", $name); ->set("picture", $picture); ->execute();
condition()
《condition(array|string $conditions, array|string|int|float $values)
》方法也可以用于向查询添加条件。它接受2个参数,第一个是条件或条件的列表,第二个是条件对应的值/值。
示例
// construct the class $query = new MySQLBuilder; // * update the user table // * update the column name to be the given new name // * update the column picture to be the given new picture // * update only for the user who has the given id // * then executed the query $query->querySelect("user") ->set("name", $name); ->set("picture", $picture); ->condition(["id = ?"], $id) ->execute();
删除值
要构造删除查询,可以使用delete(string $table)
方法。它接受1个参数。
示例
// construct the class $query = new MySQLBuilder; // * delete data from the user table $query->queryDelete("user");
execute()
execute()
方法将执行构建的查询。
示例
// construct the class $query = new MySQLBuilder; // * delete data from the user table // * then executed the query $query->queryDelete("user") ->execute();
condition()
《condition(array|string $conditions, array|string|int|float $values)
》方法也可以用于向查询添加条件。它接受2个参数,第一个是条件或条件的列表,第二个是条件对应的值/值。
示例
// construct the class $query = new MySQLBuilder; // * delete data from the user table // * only delete data that has the same id as the given id // * then executed the query $query->querySelect("user") ->condition(["id = ?"], $id) ->execute();