dimns/simplepdo

Simple PDO Wrapper class for MySQL and SQLite

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

This package is auto-updated.

Last update: 2024-04-15 02:06:31 UTC


README

Simple PDO Wrapper class for MySQL and SQLite

Latest Stable Version Total Downloads License

Requirements

  • PHP 5.3 or higher is required.
  • PHP extension MySQL or SQLite.

Composer installation

  1. Get Composer.
  2. Require SimplePDO with php composer.phar require dimns/simplepdo or composer require dimns/simplepdo (if the composer is installed globally).
  3. Add the following to your application's main PHP file: require 'vendor/autoload.php';.

Usage

// 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>';

Return values

  1. For select and show returns an array containing all of the result set rows.
  2. For insert returns the ID of the inserted row.
  3. For all other queries returns the number of rows affected by the SQL statement.