xrstf/comfydb

ComfyDB 是一个围绕 mysqli 的封装,提供一些便利函数和类似于 sprintf 的查询构造。

0.2.1 2016-06-15 19:42 UTC

This package is not auto-updated.

Last update: 2024-09-14 18:42:10 UTC


README

ComfyDB 是 PHP 的 mysqli 扩展的封装,旨在为常见任务提供便利方法,如获取单行、获取单列等。

请注意,本库不是以性能为重点。所有获取的结果集都将被全部读取,因此当您想要流式处理大型结果集时,这可能不是您想要的解决方案。

此外,本库不使用 mysqli 的预处理语句支持来处理参数化查询。相反,使用类似于 sprintf 的方法,在将查询发送到服务器之前,将占位符(如 %s%d)替换。

示例:连接

连接到数据库

use xrstf\ComfyDB;

$db = ComfyDB::connect('host', 'user', 'password', 'database');

// alternatively, wrap an existing mysqli connection
$db = new ComfyDB($mysqliConnection);

示例:查询数据

选择数据。行始终是关联数组。这里没有魔法。

$rows = $db->query('SELECT id, firstname, lastname FROM persons');

/*
$rows = [
    ['id' => 1, 'firstname' => 'Tom', 'lastname', 'Schlock'],
    ['id' => 2, 'firstname' => 'Max', 'lastname', 'Power'],
    ['id' => 3, 'firstname' => 'Maria', 'lastname', 'Gomez'],
    ...
];
*/

// use %s for strings, %d for integers, %f for floats and %n for NULLable values
// (%n will result in 'NULL' if the given value is null, otherwise it will encode
// the value as a string, like %s)
$rows = $db->query('SELECT * FROM persons WHERE firstname = %s AND id = %d', ['Tom', 1]);

从结果集中获取单列。返回值是一个包含值的扁平数组。

$names = $db->fetchColumn('SELECT firstname FROM persons WHERE 1');
// $names = ['Max', 'Tom', 'Maria'];

获取结果集,并使用第一个列作为最终结果中的键。如果只选择两列,最终映射中的值不是数组,而是第二列。

// select three columns, use ID as the key
$names = $db->fetchMap('SELECT id, firstname, lastname FROM persons WHERE 1');

/*
$names = [
    1 => ['firstname' => 'Tom', 'lastname', 'Schlock'],
    2 => ['firstname' => 'Max', 'lastname', 'Power'],
    3 => ['firstname' => 'Maria', 'lastname', 'Gomez'],
];
*/

// select two columns
$names = $db->fetchMap('SELECT id, firstname FROM persons WHERE 1');

/*
$names = [
    1 => 'Tom',
    2 => 'Max',
    3 => 'Maria',
];
*/

获取单行。如果只选择一列,则返回第一行的该值,而不是关联数组。如果没有找到行,则返回 null。如果找到多行,则只考虑第一行。

// fetch a single cell from the database
$firstname = $db->fetch('SELECT firstname FROM persons WHERE id = %d', [1]);
// $firstname = 'Tom'

// fetch more than one cell
$name = $db->fetch('SELECT firstname, lastname FROM persons WHERE id = %d', [1]);
// $name = ['firstname' => 'Tom', 'lastname', 'Schlock']

// find no rows (returns always null, disregarding of the number of columns selected)
$row = $db->fetch('SELECT * FROM table WHERE 1 = 2');
// $row = null

示例:更新数据

update() 接受表名、新数据和 WHERE 条件。

$newData = [
    'firstname' => 'Anja',
    'lastname' => 'Muster',
];

// for simple conditions, you can give the WHERE criteria as an array
$db->update('persons', $newData, ['id' => 3]);

// more complex criteria can be given as a string, which is copied verbatim
$db->update('persons', $newData, '(firstname NOT NULL OR lastname NOT LIKE "%foo")');

示例:插入数据

insert() 接受表名和新数据。

$newData = [
    'firstname' => 'Anja',
    'lastname' => 'Muster',
];

$db->insert('persons', $newData);

$id = $db->getInsertedID();

示例:删除数据

delete() 接受表名和 WHERE 条件,类似于 update()

$db->delete('persons', ['id' => 2]);
$deleted = $db->getAffectedRows();

示例:错误处理

在发生任何错误的情况下,会抛出一个 xrstf\ComfyException,其中包含错误代码、错误信息和失败的查询。

try {
    $db->delete('nope', ['id' => 2]);
}
catch (ComfyException $e) {
    print "Query: ".$e->getQuery()."\n";
    print "Code : ".$e->getCode()."\n";
    print "Error: ".$e->getErrorMessage()."\n";
}