Project is deprecated.
Simple PDO Wrapper class for MySQL and SQLite
, (*1)
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
or composer 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 '
';
print_r($result);
echo '
';
// 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 '
';
print_r($result);
echo '
';
Return values
- For
select
and show
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.