PHP Classes
elePHPant
Icontem

File: samples/pg_connstr.php

Recommend this page to a friend!
  Classes of Aleksey Nemiro  >  Nemiro Data PHP  >  samples/pg_connstr.php  >  Download  
File: samples/pg_connstr.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: 4 years ago
Size: 2,192 bytes
 

 

Contents

Class file image Download
<?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 as PgSql;
# import command class
use Nemiro\Data\DBCommand as 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>';
        }
        echo
'</tr></thead>';

       
# output the table rows
       
echo '<tbody>';
        foreach (
$table as $row)
        {
            echo
'<tr>';
            foreach (
$row as $field)
            {
                echo
'<td>'.(isset($field) ? $field : 'NULL').'</td>';
            }
            echo
'</tr>';
        }
        echo
'</tbody>';
        echo
'</table>';
    }
    else
    {
        echo
'The query returned no results...';
    }

   
# set command to client2
   
$client2->Command = $command;

   
# set a new value to the parameter (for example)
   
$command->Parameters['@limit']->SetValue(5);

   
# execute the command and get table
   
$table = $client2->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>';
        }
        echo
'</tr></thead>';

       
# output the table rows
       
echo '<tbody>';
        foreach (
$table as $row)
        {
            echo
'<tr>';
            foreach (
$row as $field)
            {
                echo
'<td>'.(isset($field) ? $field : 'NULL').'</td>';
            }
            echo
'</tr>';
        }
        echo
'</tbody>';
        echo
'</table>';
    }
    else
    {
        echo
'The query returned no results...';
    }
}
catch (
Exception $ex)
{
    echo
'Error: '.$ex->getMessage();
}
?>