1. Go to this page and download the library: Download chsxf/pdo-database-manager 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/ */
chsxf / pdo-database-manager example snippets
// Load the classes
baseManager;
// Configuration - Adapt to fit your own configuration
$server_host = 'localhost';
$user = 'username';
$password = 'password';
$database_name = 'database_name';
// Initializing database manager
$dsn = "mysql:dbname={$database_name};host={$server_host}";
$dbm = new DatabaseManager($dsn, $user, $password);
if ($dbm->query('CREATE TEMPORARY TABLE `test_table` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`label` varchar(255) COLLATE utf8_bin NOT NULL,
`price` float unsigned NOT NULL,
`stock` int(10) unsigned NOT NULL,
PRIMARY KEY (`id`)
)') === false) {
die('Unable to create temporary table');
}
// Inserting sample data
$sql = 'INSERT INTO `test_table` (`label`, `price`, `stock`) VALUE (?, ?, ?)';
$values = [
[ 'PHP for Dummies', 15.00, 100 ],
[ 'The Lord of the Rings Trilogy', 46.74, 25 ],
[ '1984', 13.42, 50 ]
];
foreach ($values as $rowValues) {
if ($dbm->exec($sql, $rowValues) === false) {
die('Unable to add sample data');
}
}
$productLabel = '1984';
$productID = $dbm->getValue('SELECT `id` FROM `test_table` WHERE `label` = ?', $productLabel);
if ($productID === false) {
die('Unable to retrieve product ID');
}
$productLabels = $dbm->getColumn('SELECT `label` FROM `test_table` ORDER BY `label` ASC');
if ($productLabels === false) {
die('Unable to retrieve product labels');
}
echo "Product labels:\n";
foreach ($productLabels as $label) {
echo "\t- {$label}\n";
}
$productData = $dbm->getRow('SELECT * FROM `test_table` WHERE `id` = ?', \PDO::FETCH_ASSOC, $productID);
if ($productData === false) {
die('Unable to retrieve product data');
}
echo "Specific row (as an associative array):\n";
foreach ($productData as $k => $v) {
echo "\t- {$k}: {$v}\n";
}
$productData = $dbm->getIndexed('SELECT * FROM `test_table`', 'id', \PDO::FETCH_OBJ);
if ($productData === false) {
die('Unable to retrieve product data');
}
echo "Indexed rows (as objects):\n";
foreach ($productData as $id => $row) {
echo "\tProduct with ID #{$id}:\n";
foreach ($row as $k => $v) {
echo "\t\t- {$k}: {$v}\n";
}
}
$productPairs = $dbm->getPairs('SELECT `id`, `price` FROM `test_table` ORDER BY `price` DESC');
if ($productPairs === false) {
die('Unable to retrieve price per ID');
}
echo "Pairs of values:\n";
foreach ($productPairs as $k => $v) {
echo "\t- Price for product #{$k}: {$v}\n";
}
define('chsxf\PDO\ERRORS_TABLE', 'my_custom_error_table_name');
use \chsxf\PDO\DatabaseManager;
Several rows (as numerically-indexed arrays):
Row #0:
- 0: 1
- 1: PHP for Dummies
- 2: 15
- 3: 100
Row #1:
- 0: 2
- 1: The Lord of the Rings Trilogy
- 2: 46.74
- 3: 25
Row #2:
- 0: 3
- 1: 1984
- 2: 13.42
- 3: 50
sql
CREATE TABLE `database_errors` (
`query` text COLLATE utf8_bin NOT NULL,
`error_code` int(11) NOT NULL,
`error_message` text COLLATE utf8_bin NOT NULL,
`file` text COLLATE utf8_bin NOT NULL,
`line` int(11) NOT NULL,
`function` text COLLATE utf8_bin NOT NULL,
`class` text COLLATE utf8_bin NOT NULL
);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.