1. Go to this page and download the library: Download shlikhota/postgresdb-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.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shlikhota / postgresdb-php example snippets
use PostgresDB\Driver as DB;
...
echo DB::fetchOne('SELECT ?', 'hello from postgres');
use DB;
...
echo DB::fetchOne('SELECT ?', 'hello from postgres');
/*
SELECT u.username, u.email, r.name
FROM users AS u
INNER JOIN roles AS r ON r.id = u.role_id
WHERE u.username LIKE 'test%'
AND created_at BETWEEN '2015-06-08' AND '2015-12-30'
[AND u.active = 1]
ORDER BY id DESC
[LIMIT $limit]
*/
$users = DB::select('u.username', 'u.email', 'r.name')
->from('users AS u')
->innerJoin('roles AS r', 'r.id = u.role_id')
->where('u.username LIKE ?', 'test%')
->where('created_at BETWEEN ? AND ?', ['2015-06-08', '2015-12-30']);
if ($only_active) {
$users->where('u.active', 1);
}
if (is_int($limit)) {
$users->limit($limit);
}
$users->order('id', 'desc');
$result = $users->fetchAll();
// UPDATE users SET active = 1 WHERE deleted = 0 AND active = 0
DB::update('users', ['active' => 1], ['deleted' => 0, 'active' => 0]);
// DELETE FROM users WHERE active = 0
DB::delete('users', ['create_at BETWEEN ? AND ?' => ['2015-06-08', '2015-12-30']]);
$lately = '1 hour';
$event_type = 1;
$event_more_than = 10;
DB::query('
WITH rank_up AS (
SELECT ue.id AS group_id, COUNT(*)
FROM users_events AS ue
INNER JOIN users_groups AS ug ON ug.user_id = ue.user_id
WHERE ue.created_at > now() - interval ? AND
ue.event_type_id = ?
GROUP BY ue.id
HAVING COUNT(*) > ?
)
UPDATE users_groups SET rank = rank + 1 WHERE id IN (SELECT group_id FROM rank_up)
', [$lately, $event_type, $event_more_than]);
DB::transaction(function($db){
// This will occur in the transaction
$user_id = $db->select('id')->from('users')->fetchOne();
$db->delete('users', ['id' => $user_id]);
});