1. Go to this page and download the library: Download falseclock/dbd-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?phprequire_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
falseclock / dbd-php example snippets
useDBD\Common\Config;
useDBD\Pg;
$config = new Config("127.0.0.1", 5432, "db_name", "user_name", "user_password");
$dbh = new Pg($config);
$dbh->connect();
/*
... do some stuff
*/
$dbh->disconnect();
resource connect ()
int do ( string $statement [, mixed $params ] )
useDBD\Common\Config;
useDBD\Pg;
$config = new Config("127.0.0.1", 5432, "db_name", "user_name", "user_password");
$db = new Pg($config);
$db->connect();
// The following example is insecure against SQL injections
$param = "'must be null'";
$result = $db->do("UPDATE table SET column1 = NULL WHERE column2 = $param");
// more easiest, simple and safe for SQL injections example.// Number of affected tuples will be stored in $result variable
$result = $db->do("UPDATE table SET column1 = ? WHERE column2 = ?", NULL, 'must be null');
useDBD\Pg;
/** @var Pg $db */
$sth = $db->query("SELECT * FROM invoices");
while ($row = $sth->fetchRow()) {
echo($row['invoice_id']);
}
$sth = $db->query("UPDATE invoices SET invoice_uuid = ?",'550e8400-e29b-41d4-a716-446655440000');
echo($sth->affectedRows());
resource prepare ( string $statement )
useDBD\Pg;
/** @var Pg $db */// Common usage for repeatedly SELECT queries
$sth = $db->prepare("UPDATE table SET column1 = ? WHERE column2 = ?");
$fruits = array('apple','banana','apricot');
foreach ($fruits as $fruit) {
$sth->execute(NULL,$fruit);
}
/*
this code will execute three statements
UPDATE table SET column1 = NULL WHERE column2 = 'apple';
UPDATE table SET column1 = NULL WHERE column2 = 'banana';
UPDATE table SET column1 = NULL WHERE column2 = 'apricot';
*/
resource execute ( [ mixed $params ] )
useDBD\Pg;
/** @var Pg $db */// Common usage for repeatedly UPDATE queries
$sth = $db->prepare("SELECT col1, col2, col3 FROM table1");
$std = $db->prepare("UPDATE table2 SET col2 =? WHERE col1=? AND col2=?");
$sth->execute();
while ($row = $sth->fetchRow()) {
if ($row['col1'] == 'banana') {
$std->execute(FALSE, NULL, $row['col2']);
}
}
/*
this code will execute this statement
UPDATE table2 SET col2 = FALSE WHERE col1 = NULL AND col2 = <value of col2 from table1>;
*/
mixed fetch ()
useDBD\Pg;
/** @var Pg $db */
$sth = $db->prepare("SELECT 'VIR-TEX LLC' AS company, generate_series AS wrh_id, 'Warehouse #'||trunc(random()*1000) AS wrh_name, trunc((random()*1000)::numeric, 2) AS wrh_volume FROM generate_series(1,3)");
/* select result example
company | wrh_id | wrh_name | wrh_volume
-------------+--------+----------------+------------
VIR-TEX LLP | 1 | Warehouse #845 | 489.20
VIR-TEX LLP | 2 | Warehouse #790 | 241.80
VIR-TEX LLP | 3 | Warehouse #509 | 745.29
*/
$sth->execute();
$company_name = $sth->fetch(); // getting first column
$wrh_id = $sth->fetch(); // getting second column as an example of subsequent invoking
$wrh_name = $sth->fetch(); // getting third columnecho ("Company name: $company_name\n");
while ($row = $sth->fetchRow()) {
echo(" {$row['wrh_name']} volume {$row['wrh_volume']}\n");
}
/* cycle above will produce following printout
Company name: VIR-TEX LLP
Warehouse #845 volume: 489.20
Warehouse #790 volume: 241.80
Warehouse #509 volume: 745.29
*/
array fetchRow ()
useDBD\Pg;
/** @var Pg $db */
$sth = $db->prepare("SELECT *, 'orange' AS col1, 'apple' AS col2, 'tomato' AS col3 FROM generate_series(1,3)");
$sth->execute();
print_r($sth->fetchrow());
/* code above will produce following printout
Array
(
[generate_series] => 1
[col1] => orange
[col2] => apple
[col3] => tomato
)
*/