PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Martin Alterisio   DATA   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: An usage example
Class: DATA
Access data stored in MySQL tables like arrays
Author: By
Last change: v0.8:
- changed how array offsets are mapped to the conditions of selecting a row (for possible security problems). If available, an unique field is used, and access using the pk is provided using the special property ->byXXXX
- abstraction of sql data types and inboxing of these values into objects with constraints checking
- disabling of inboxing of sql data types is available through property ->withoutInboxing
- new exceptions hierarchy (allows more fine-grained catching of exceptions)
Date: 17 years ago
Size: 3,387 bytes
 

Contents

Class file image Download
<?php
/**
 * @ignore
 */

/**
 * @ignore
 */
require_once('data.mysql5.lib.php');

echo
"Connecting to database...\n";
$DB->connect();
$DB->selectSchema('test');

echo
"Creating test table...\n";
$DB->query('DROP TABLE IF EXISTS `test_table`');
$DB->query('
    CREATE TABLE `test_table` (
      `id` int(11) NOT NULL auto_increment,
      `name` varchar(20) NOT NULL,
      `value` varchar(80) NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `name` (`name`)
    )
'
);

echo
"Insert rows.\n";
$DB['test_table'][] = array(
   
'name' => 'first',
   
'value' => 'the first row'
);
$DB['test_table'][] = array(
   
'name' => 'second',
   
'value' => 'the second row'
);
$DB['test_table'][] = array(
   
'name' => 'third',
   
'value' => 'the third row'
);

echo
"for loop:\n";
for (
$i = 0; $i < count($DB['test_table']); $i++) {
   
$row = $DB['test_table'][$i];
    echo
" $i: {$row['id']}, {$row['name']}, {$row['value']}\n";
}

echo
"foreach loop:\n";
foreach (
$DB['test_table'] as $i => $row) {
    echo
" $i: {$row['id']}, {$row['name']}, {$row['value']}\n";
}

echo
"Modify rows...\n";

echo
" by int primary key, field access.\n";
$DB['test_table']->byId['2']['value'] = '2nd';
$row = $DB['test_table']->byId['2'];
echo
" {$row['id']}, {$row['name']}, {$row['value']}\n";

echo
" by string unique field, field access.\n";
$DB['test_table']['third']['value'] = '3rd';
$row = $DB['test_table']['third'];
echo
" {$row['id']}, {$row['name']}, {$row['value']}\n";

echo
" by int primary key, row access.\n";
$DB['test_table']->byId['2'] = array('value' => 'second value');
$row = $DB['test_table']->byId['2'];
echo
" {$row['id']}, {$row['name']}, {$row['value']}\n";

echo
" by string unique field, row access.\n";
$DB['test_table']['third'] = array('value' => 'third value');
$row = $DB['test_table']['third'];
echo
" {$row['id']}, {$row['name']}, {$row['value']}\n";

echo
"Clone row...\n";

$clone = clone $DB['test_table']['first'];
$clone['id'] = null;
$clone['name'] = 'fourth';
$clone['value'] = 'fourth value';
$row = $DB['test_table']['first'];
echo
" {$clone['id']}, {$clone['name']}, {$clone['value']}\n";
echo
" {$row['id']}, {$row['name']}, {$row['value']}\n";

echo
"Insert clone...\n";

$DB['test_table'][] = $clone;
echo
" {$clone['id']}, {$clone['name']}, {$clone['value']}\n";

echo
"Modify row by cloning...\n";

echo
" by string unique field.\n";
$clone = clone $DB['test_table']['second'];
$clone['value'] = 'second value';
$DB['test_table']['second'] = $clone;
$row = $DB['test_table']['second'];
echo
" {$row['id']}, {$row['name']}, {$row['value']}\n";

echo
" by int primary key.\n";
$clone = clone $DB['test_table']->byId['1'];
$clone['value'] = 'first value';
$DB['test_table']->byId['1'] = $clone;
$row = $DB['test_table']->byId['1'];
echo
" {$row['id']}, {$row['name']}, {$row['value']}\n";

echo
"Delete rows...\n";

echo
" by int primary key.\n";
unset(
$DB['test_table']->byId['1']);
try {
   
$row = $DB['test_table']->byId['1'];
} catch (
DATA_RowDoesntExist $e) {
    echo
" row deleted.\n";
}

echo
" by string unique field.\n";
unset(
$DB['test_table']['third']);
try {
   
$row = $DB['test_table']['third'];
} catch (
DATA_RowDoesntExist $e) {
    echo
" row deleted.\n";
}

?>