dekyfin/easypdo

PDO 的便捷包装器。目前仅支持 MySQL

1.3.1 2018-11-16 18:12 UTC

This package is auto-updated.

Last update: 2024-09-17 09:50:52 UTC


README

PDO 的便捷包装器。目前仅支持 MySQL。旨在简化常见查询,以减少需要编写的代码。

示例

// Insert row using prepared statement and get insert id
// PDO
$stmt = $db->prepare("INSERT INTO table(col1,col2) VALUES(:col1, :col2)");
$stmt->execute(["col1"=>"val1", "col2"=>"val2"]);
$id = $db->lastInsertId();

// EasyPDO
$id = $db->insert("table", ["col1" => "val1", "col2"=>"val2"]);


// Select rows from DB
//PDO
$stmt2 = $db->prepare("SELECT * FROM table WHERE col1 = :col1 AND col2 = :col2)");
$stmt2->execute(["col1"=>"val1", "col2"=>"val2"]);
$rows = $db->fetchAll( $stmt2 );

// EasyPDO
$rows = $db->select("table", ["col1"=>"val1", "col2"=>"val2"]);

EasyPDO 能做什么?

  1. 所有 CRUD 操作的便捷包装器
    1. SELECT
    2. UPDATE
    3. INSERT
    4. DELETE
    5. INSERT/UPDATE
  2. 轻松获取 INSERT 的 insertId、UPDATE/DELETE 的 affected rows 和 SELECT 语句的行数组。
/*	Take note of the  the 2nd parameter:
*	A value of true ocauses the function to return the 'convenient data', 
*	while false returns the PDOStatement Object
*/

// Select Statement
$rows = $db->execute("SELECT id FROM table WHERE id < :max", ["max"=>3], true);
// Output: array of matched rows

$insertId = $db->execute("INSERT INTO table(col1, col2) VALUES(:col1, :col2)", ["col1"=>"val1", "col2"=>"val2"], true);
//Output: inserId

$affectedRows = $db->execute("DELETE FROM table WHERE id = :id", ["id"=>5], true);
//Output: affected rows

$affectedRows = $db->execute("UPDATE table SET col1 = :col1 WHERE id = :id", ["id"=>5, "col1"=>"valX"], true);
//Output: affected rows
  1. 任何时候都可以使用原始的 PDO

安装

Composer

composer require dekyfin/easypdo

手动

  1. 下载 zip 文件
  2. src/DB.php 包含到您的项目中
require_once "/path/to/src/DB.php";

用法

// Options for connecting to MySQL database
$options = [
	"host"=>"localhost",
	"user"=>"db_user",
	"db"=>"db_name",
	"pass"=>"s3cr3tp@ssw0rd"
];

// Create connection
$db = new DF\DB($options);

// Run queries
$rows = $db->query("SELECT * FROM table", true);

方法

select( string $table, array $conditions = [] , mixed $modifiers ): array $rows

用于选择匹配某些 $conditions 的行。此方法当前选择所有列。将在 v2.x.x 版本中修改,以允许指定列

  • $conditions 一个关联数组,其中 $column => $value 用于匹配。使用 SQL AND 操作符来匹配所有 $conditions
  • $modifiers int || string
    • 一个整数将限制结果数量。结果为 LIMIT BY $modifiers
    • 一个字符串,将放在查询末尾以修改行为。例如:ORDER BY id DESC
$data = $db->select("table", ["category"=>3] , 10); //Show only 10 results
$data = $db->select("table", ["category"=>3] , "ORDER BY id DESC");

insert( string $table, array $values ): int $insertId

update( string $table, array $values = [] [, array $conditions] ): int $rowCount

insertUpdate( string $table, array $values ): int insertId

注意:如果 insert 或 update 操作成功,则此方法将返回行的 insertId

delete( string $table, array $conditions): int $rowCount

query( string $sql , boolean $fetchAll = false )

此函数用于运行 SQL 查询。如果 $fetchAll 为 true,则返回数据数组,如果为 false,则返回 PDOStatement 对象

$data = $db->query("Select id FROM table", true); // [ [id=>1], [id=>2] ... ]

execute( string $sql, array $values , boolean $fetchAll = false )

用于准备和执行语句。对于运行一次性的准备语句非常有用。如果 $fetchAll 为 true,则返回数据数组,如果为 false,则返回 PDOStatement 对象

$data = $db->execute("Select id FROM table WHERE id < :max", ["max"=>3] , true); // [ [id=>1], [id=>2] ... ]

prepare( string $sql ): PDOStatement

$stmt = $db->prepare("INSERT INTO table(col1,col2) VALUES(:col1, :col1)");

$stmt->execute(["col1" => 3, "col2" => 4 ]);
$stmt->execute(["col1" => 5, "col2" => 6 ]);
$stmt->execute(["col1" => 50, "col2" => 2e6 ]);