PHP Classes

File: samples/pg_insert_get_id.php

Recommend this page to a friend!
  Classes of Aleksey Nemiro   Nemiro Data PHP   samples/pg_insert_get_id.php   Download  
File: samples/pg_insert_get_id.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Nemiro Data PHP
Compose and execute MySQL or PostgreSQL queries
Author: By
Last change:
Date: 7 years ago
Size: 1,432 bytes


Class file image Download

# 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 as PgSql;
# import command class
use Nemiro\Data\DBCommand as DBCommand;

# best practice is to use 'try { } catch { }' blocks
# 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
# execute the command
$newId = $client->ExecuteScalar();

'New row added. ID = '.$newId.'<br />';

# you can change the parameters and execute the command again
$newId = $client->ExecuteScalar();

'New row added. ID = '.$newId.'<br />';

# and again
$newId = $client->ExecuteScalar();

'New row added. ID = '.$newId.'<br />';
catch (
Exception $ex)
'Error: '.$ex->getMessage();