PHP Classes
Icontem

File: example.sqltypes.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Martin Alterisio  >  DATA  >  example.sqltypes.php  
File: example.sqltypes.php
Role: Example script
Content type: text/plain
Description: An usage example
Class: DATA
Access data stored in MySQL tables like arrays
 

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,
      `charfield` char(20) NOT NULL,
      `nullcharfield` char(20) NULL,
      `varcharfield` varchar(20) NOT NULL,
      `intfield` int NOT NULL,
      `nullintfield` int NULL,
      `smallintfield` smallint NOT NULL,
      `decimalfield` decimal(7,3) NOT NULL,
      `datefield` date NULL,
      `timefield` time NULL,
      `datetimefield` datetime NULL,
      PRIMARY KEY  (`id`),
      UNIQUE KEY `name` (`name`)
    )
'
);

echo 
"Insert valid row...\n";
$DB['test_table'][] = array(
    
'name'          => 'test',
    
'charfield'     => '20 char field',
    
'nullcharfield' => null,
    
'varcharfield'  => '20 varchar field',
    
'intfield'      => 101,
    
'nullintfield'  => null,
    
'smallintfield' => 11,
    
'decimalfield'  => '123.45',
    
'datefield'     => DATA_SQLDate::today(),
    
'timefield'     => DATA_SQLTime::now(),
    
'datetimefield' => DATA_SQLDatetime::now(),
);
foreach (
$DB['test_table']['test'] as $name => $value) {
    if (
DATA_SQLType::isNull($value)) {
        echo 
"    $name: NULL\n";
    } else {
        echo 
"    $name: |$value|\n";
    }
}

echo 
"Insert invalid char(20) field...\n";
echo 
"  null value...\n";
try {
    
$DB['test_table'][] = array(
        
'name'          => 'test2',
        
'charfield'     => null,
    );
catch (DATA_SQLTypeConstraintFailed $e) {
    echo 
"    exception " get_class($e) . " on table `" $e->getTable() . "` and field `" $e->getField() . "`\n";
}
echo 
"  'too much characters for this field'...\n";
try {
    
$DB['test_table'][] = array(
        
'name'          => 'test2',
        
'charfield'     => 'too much characters for this field',
    );
catch (DATA_SQLTypeConstraintFailed $e) {
    echo 
"    exception " get_class($e) . " on table `" $e->getTable() . "` and field `" $e->getField() . "`\n";
}

echo 
"Insert invalid int field...\n";
echo 
"  null value...\n";
try {
    
$DB['test_table'][] = array(
        
'name'          => 'test2',
        
'intfield'      => null,
    );
catch (DATA_SQLTypeConstraintFailed $e) {
    echo 
"    exception " get_class($e) . " on table `" $e->getTable() . "` and field `" $e->getField() . "`\n";
}
echo 
"  'not a number'...\n";
try {
    
$DB['test_table'][] = array(
        
'name'          => 'test2',
        
'intfield'      => 'not a number',
    );
catch (DATA_SQLTypeConstraintFailed $e) {
    echo 
"    exception " get_class($e) . " on table `" $e->getTable() . "` and field `" $e->getField() . "`\n";
}
echo 
"  '12345678901234567890'...\n";
try {
    
$DB['test_table'][] = array(
        
'name'          => 'test2',
        
'intfield'      => '12345678901234567890',
    );
catch (DATA_SQLTypeConstraintFailed $e) {
    echo 
"    exception " get_class($e) . " on table `" $e->getTable() . "` and field `" $e->getField() . "`\n";
}

echo 
"Insert invalid smallint field...\n";
echo 
"  '1234567'...\n";
try {
    
$DB['test_table'][] = array(
        
'name'          => 'test2',
        
'smallintfield' => '1234567',
    );
catch (DATA_SQLTypeConstraintFailed $e) {
    echo 
"    exception " get_class($e) . " on table `" $e->getTable() . "` and field `" $e->getField() . "`\n";
}

echo 
"Insert invalid decimal(7,3) field...\n";
echo 
"  '12345'...\n";
try {
    
$DB['test_table'][] = array(
        
'name'          => 'test2',
        
'decimalfield'  => '12345',
    );
catch (DATA_SQLTypeConstraintFailed $e) {
    echo 
"    exception " get_class($e) . " on table `" $e->getTable() . "` and field `" $e->getField() . "`\n";
}
echo 
"  '0.1234'...\n";
try {
    
$DB['test_table'][] = array(
        
'name'          => 'test2',
        
'decimalfield'  => '0.1234',
    );
catch (DATA_SQLTypeConstraintFailed $e) {
    echo 
"    exception " get_class($e) . " on table `" $e->getTable() . "` and field `" $e->getField() . "`\n";
}

echo 
"Insert invalid date field...\n";
echo 
"  '2007-02-31'...\n";
try {
    
$DB['test_table'][] = array(
        
'name'          => 'test2',
        
'datefield'     => '2007-02-31',
    );
catch (DATA_SQLTypeConstraintFailed $e) {
    echo 
"    exception " get_class($e) . " on table `" $e->getTable() . "` and field `" $e->getField() . "`\n";
}
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products