PClib demo site

<?php
/* project: Database layer, file: db.php */

//Include pclib framework
require 'pclib/pclib.php';

print 
"<h2>Using pclib database layer</h2>";
print 
"(Look at the source code for short tutorial)";

$datasource 'pdo_mysql://user:password@localhost/test';

// -- Instantiate class db
$db = new PCDb($datasource);

print 
"<h4>TEST-1 db->query()/db->fetch()</h4>";

// Executing queries and fetching results....
$res    $db->query('select * from PEOPLE');
$array  $db->fetch($res);
$object $db->fetch($res'o');
$rows   $db->fetchAll($res);

//Export result rows to html table...
print "HTML table generated by db->export() function:";
print 
$db->export($rows);


print 
"<h4>TEST-2 db->select*()</h4>";

//Instead of plain query, it is possible use "shortcut" functions
$array $db->select('PEOPLE'); //SELECT * FROM PEOPLE LIMIT 1
print $db->export($arr);

$allrows $db->selectAll('PEOPLE'); //SELECT * FROM PEOPLE
print $db->export($allrows);

//Each of db->select_* functions can take the same parameters

//return array('Bill', 'Jack', 'John' ...)
$arr $db->selectOne('PEOPLE:NAME'); //SELECT NAME FROM PEOPLE

//return array with ID => NAME pairs (10 => 'Bill', 12 => 'Jack', 14 => 'John' ...)
$arr $db->selectPair('PEOPLE:ID,NAME');

//Counting rows: this command return number of rows in last query result
print "Number of rows in last query: ";
print 
$db->count();

//In any select_* function you can add WHERE condition

$arr $db->select('PEOPLE''ID=1'); //SELECT * FROM PEOPLE WHERE ID=1

//You can use "shortcut" functions for INSERT,UPDATE,DELETE queries too.

//We don't want do real changes in table, so we disable query executing for now
$db->disabled true;

$person = array (
 
'NAME' => 'Bill',
 
'SURNAME' => 'Gates',
 
'MONEY' => 1000000000
);

//return last_inserted_id
$person_id $db->insert('PEOPLE'$person); //INSERT INTO PEOPLE (NAME,SURNAME,MONEY) VALUES ('Bill','Gates','1000000000')

$db->update('PEOPLE'$person'ID=1'); //UPDATE PEOPLE set NAME='Bill',SURNAME='Gates',MONEY='1000000000' WHERE ID=1
$db->delete('PEOPLE''ID=1'); //DELETE FROM PEOPLE WHERE ID=1

// -- Query parameters

//You can put parameters into queries
// (See "Examples of using database functions parameters"
// and "Dynamic SQL" page in reference manual - section "Related pages")

$db->disabled false;

$id 1;
$money 1000;

$res $db->query("select * from PEOPLE where ID='{0}' and MONEY>'{1}'"$id$money);
$arr $db->select('PEOPLE'"ID='{0}' and MONEY>'{1}'"$id$money);

//PARAMETERS IN ARRAY
$params = array('ID' => 1'MONEY' => 1000);

$res $db->query("select * from PEOPLE where ID='{ID}' and MONEY>'{MONEY}'"$params);
$arr $db->select('PEOPLE'"ID='{ID}' and MONEY>'{MONEY}'"$params);

print 
"<h4>Show last executed query</h4>";
print 
$db->lastQuery;

$db->disabled true;
$output = [];

$xval "Xval";
$yval "Yval";

$db->select("select * from A where X='{0}' and Y='{1}'"$xval$yval);
$output[] = $db->lastQuery;
$db->select("select * from A where X='{0}' and Y='{1}'", [$xval$yval]);
$output[] = $db->lastQuery;
$db->select("select * from A where X='{X}' and Y='{Y}'", ['X' => $xval'Y' => $yval]);
$output[] = $db->lastQuery;

$db->select("select * from A where X='{#X}' and Y='{#Y}'", ['X' => $xval'Y' => $yval]);
$output[] = $db->lastQuery;
$db->select("select * from A where X='{#X}' and Y='{#Y}'", ['X' => '100''Y' => 200]);
$output[] = $db->lastQuery;
$db->select("select * from A where X='{#X}' and Y='{#Y}'", ['X' => '100abc''Y' => 20.2]);
$output[] = $db->lastQuery;

$db->select("select * from A where X in ('{0}')", [[$xval$yval]]);
$output[] = $db->lastQuery;
$db->select("select * from A where X in ('{X}')", ['X' => [$xval$yval]]);
$output[] = $db->lastQuery;

$db->select("select * from A where X in ({#0})", [[$xval$yval]]);
$output[] = $db->lastQuery;
$db->select("select * from A where X in ({#0})", [['100'200]]);
$output[] = $db->lastQuery;

$db->select(
    
"select * from A where X='{X}'
    ~ and Y='{Y}'"
, ['X' => $xval]);
$output[] = $db->lastQuery;

$db->select(
    
"select * from A where X='{X}'
    ~ and Y='{Y}'"
, ['X' => $xval'Y' => '']);
$output[] = $db->lastQuery;

$db->select(
    
"select * from A where X='{X}'
    ~ and Y='{Y}'"
, ['X' => $xval'Y' => $yval]);
$output[] = $db->lastQuery;

$db->insert('A',  ['X' => $xval'Y' => $yval]);
$output[] = $db->lastQuery;

// $db->insert('A',  [$xval => $xval, $yval => $yval]);
// $output[] = $db->lastQuery;

$db->update('A',  ['X' => $xval'Y' => $yval], ['X' => $xval'Y' => $yval]);
$output[] = $db->lastQuery;

$db->delete('A',  ['X' => $xval'Y' => $yval]);
$output[] = $db->lastQuery;


print 
'<br>';
print 
implode('<br>'$output);



?>

Elapsed time: 14.22 ms