dimns / simplepdo
Simple PDO Wrapper class for MySQL and SQLite
v1.2.0
2016-06-28 05:46 UTC
Requires
- php: >=5.3.0
This package is auto-updated.
Last update: 2024-11-15 03:42:34 UTC
README
Simple PDO Wrapper class for MySQL and SQLite
Requirements
- PHP 5.3 or higher is required.
- PHP extension MySQL or SQLite.
Composer installation
- Get Composer.
- Require SimplePDO with
php composer.phar require dimns/simplepdo
orcomposer require dimns/simplepdo
(if the composer is installed globally). - 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
- For
select
andshow
returns an array containing all of the result set rows. - For
insert
returns the ID of the inserted row. - For all other queries returns the number of rows affected by the SQL statement.