dimns/simplepdo

MySQL 和 SQLite 的 Simple PDO 包装类

v1.2.0 2016-06-28 05:46 UTC

This package is auto-updated.

Last update: 2024-09-15 03:08:38 UTC


README

MySQL 和 SQLite 的 Simple PDO 包装类

Latest Stable Version Total Downloads License

需求

  • 需要 PHP 5.3 或更高版本。
  • 需要 PHP 扩展 MySQL 或 SQLite。

Composer 安装

  1. 获取 Composer
  2. 使用以下命令安装 SimplePDO: php composer.phar require dimns/simplepdocomposer require dimns/simplepdo(如果 composer 已全局安装)。
  3. 将以下代码添加到您应用程序的主 PHP 文件中: require 'vendor/autoload.php';

用法

// Init class for MySQL (default port 3306)
$db = new DimNS\SimplePDO\MySQL('server', 'dbname', 'username', 'password');
// Or init class for MySQL (override port)
$db = new DimNS\SimplePDO\MySQL('server', 'dbname', 'username', 'password', 3307);
// Or init class for SQLite
$db = new DimNS\SimplePDO\SQLite('/path/to/database/file.sqlite');

// Query without prepared variables
$result = $db->query('SELECT `field1`, `field2` FROM `table`');
echo '<pre>';
print_r($result);
echo '</pre>';

// Query with prepared variables
$result = $db->query('INSERT INTO `table` SET
    `field1` = :field1,
    `field2` = :field2
', [
    'field1' => 'Simple string',
    'field2' => 123,
]);
echo $result;

// Transaction (only for mysql innodb table)
$result = $db->transaction([
    [
        'query' => 'INSERT INTO `table` SET `field1` = :field1, `field2` = :field2, `field3` = :field3',
        'data'  => [
            'field1' => 'val1',
            'field2' => 'val2',
            'field3' => 'val3',
        ],
    ], [
        'query' => 'UPDATE `table` SET `field1` = :field1 WHERE `field2` > :field2',
        'data'  => [
            'field1' => 'val1',
            'field2' => 'val2',
        ],
    ],
]);
echo '<pre>';
print_r($result);
echo '</pre>';

返回值

  1. 对于 selectshow 返回包含所有结果集行的数组。
  2. 对于 insert 返回插入行的 ID。
  3. 对于所有其他查询返回受 SQL 语句影响的行数。