1. Go to this page and download the library: Download phossa/phossa-db library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
phossa / phossa-db example snippets
$db = new Phossa\Db\Pdo\Driver([
'dsn' => 'mysql:dbname=test;host=127.0.0.1;charset=utf8'
]);
// simple delete
$res = $db->execute("DELETE FROM test WHERE id < 10");
if (false === $res) {
echo $db->getError() . \PHP_EOL;
} else {
echo sprintf("Deleted %d records", $res) . \PHP_EOL;
}
// with parameters
$res = $db->execute("INSERT INTO test (name) VALUES (?)", [ 100 ]);
if ($res) {
$id = (int) $db->getLastInsertId();
}
// simple select
$res = $db->query("SELECT * FROM test WHERE id < 10");
if (false === $res) {
echo $db->getError() . \PHP_EOL;
} else {
$rows = $res->fetchAll();
}
// with parameters & fetch first 5 rows
$res = $db->query("SELECT * FROM test WHERE id > ? LIMIT ?", [10, 20]);
if ($res && $res->isQuery()) {
$firstFiveRows = $res->fetchRow(5);
}
// fetch first field
$res = $db->query("SELECT id, name FROM test WHERE id < :id", ['id' => 10]);
if ($res && $res->isQuery()) {
$firstCols = $res->fetchCol('id');
}
// PREPARE using prepare()
$stmt = $db->prepare("SELECT * FROM test WHERE id < :id");
if (false === $stmt) {
echo $db->getError() . \PHP_EOL;
} else {
$res = $stmt->execute(['id' => 10]);
if (false === $res) {
echo $db->getError() . \PHP_EOL;
} else {
$rows = $res->fetchAll();
}
}