<?php # include config file require_once 'config.php'; # include the db client classes file (use own path of the file location) require_once '../Import.php'; # import client for PostgreSQL use Nemiro\Data\PgSql; # import command class use Nemiro\Data\DBCommand; # best practice is to use 'try { } catch { }' blocks try { # create client instance $client = new PgSql(); # create a new command with parameters $client->Command = new DBCommand('INSERT INTO users (username, date_created) VALUES (@username, @date_created) RETURNING id_users;'); # @username and @date_created is parameters name, # you can add a values for this parameters $client->Command->Parameters->Add('@username')->SetValue('anyname'); $client->Command->Parameters->Add('@date_created')->SetValue(date('d.m.Y')); # execute the command $newId = $client->ExecuteScalar(); echo 'New row added. ID = ' . $newId . '<br />'; # you can change the parameters and execute the command again $client->Command->Parameters['@username']->SetValue('newValue'); $newId = $client->ExecuteScalar(); echo 'New row added. ID = ' . $newId . '<br />'; # and again $client->Command->Parameters['@username']->SetValue('123'); $client->Command->Parameters['@date_created']->SetValue(date('d.m.Y')); $newId = $client->ExecuteScalar();
<?php # include config file require_once 'config.php'; # include the db client classes file (use own path of the file location) require_once '../Import.php'; # import client for PostgreSQL use Nemiro\Data\PgSql; # import command class use Nemiro\Data\DBCommand; # import parameters type list use Nemiro\Data\DBParameterType; # best practice is to use 'try { } catch { }' blocks try { # create client instance $client = new PgSql(); # you can specify the text of the query into the Command property $client->Command = 'SELECT * FROM users'; # and execute the query by any method: # $client->GetRow(); # return single row # $client->GetTable(); # return all rows # $client->GetData(); # return array tables # for example, get all rows $table = $client->GetTable(); var_dump($table); # you can build a query from the input parameters, # but you will need to check the type and value of incoming data # it is bad practice $client->Command = 'DELETE FROM users WHERE id_users = ' . (int) $_GET['id']; # it is best to use parameterized queries $client->Command = new DBCommand('DELETE FROM users WHERE id_users = @id_users');
<?php # include config file require_once 'config.php'; # include the db client classes file (use own path of the file location) require_once '../Import.php'; # import client for PostgreSQL use Nemiro\Data\PgSql; # import command class use Nemiro\Data\DBCommand; # best practice is to use 'try { } catch { }' blocks try { # create client instance with connection settings from config.php $client = new PgSql(); # create client instance with specifed connection settings $client2 = new PgSql('localhost', 'test', '5QKfHhB323EM', 'test_db', 5432); # create command $command = new DBCommand('SELECT * FROM users LIMIT @limit'); $command->Parameters->Add('@limit', 10); # set command to client $client->Command = $command; # execute the command and get table $table = $client->GetTable(); if (count($table) > 0) { # output columns of the table $columns = array_keys($table[0]); echo '<table>'; echo '<thead><tr>'; foreach ($columns as $column) { echo '<td>' . $column . '</td>'; }