PHP Classes

File: test.php

Recommend this page to a friend!
  Classes of Marko Tapio Manninen   PhpDtObject   test.php   Download  
File: test.php
Role: Example script
Content type: text/plain
Description: Test & example script
Class: PhpDtObject
Database table access classes for php
Author: By
Last change: Typo corrections...
Date: 19 years ago
Size: 4,315 bytes
 

Contents

Class file image Download
<?php
/**
 * @author Marko Manninen <marko.manninen@hmv-systems.fi>
 * @copyright Copyright © 2004, Marko Manninen
 * @date 9.6. -2004
 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License
 */
/**
 * Include classes for the test. ADOdb can be downloaded from: http://php.weblogs.com/adodb
 */
include_once( "./adodb/adodb.inc.php" );
include_once(
'DBConn.php' );
include_once(
'PhpDtObject.php' );
/**
 * To get these examples working, you need to include above classes, set up DBConn class and create
 * next table with INSERT clauses to the table.
 *
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(32) NOT NULL default '',
  `age` int(3) NOT NULL default '0',
  `date` date NOT NULL default '0000-00-00',
  PRIMARY KEY (`id`)
) TYPE=MyISAM AUTO_INCREMENT=4 ;

INSERT INTO `test` VALUES (1, 'Tester', 32, '2004-06-09');
INSERT INTO `test` VALUES (2, 'Quaker', 99, '2004-06-09');
INSERT INTO `test` VALUES (3, 'Bobby', 5, '2004-06-09');
 */
/**
 * Create a new dbconn object with a table name.
 *
 * You can set database connection variables straight to the class or by setting public field variables
 * after constructing the dbconn object, but BEFORE connect method. Ie.
 *
 * $dbconn = new DBConn( "myTable" );
 * $dbconn->driver = "postgre";
 * $dbconn->server = "localhost";
 * $dbconn->db = "myDatabase";
 * $dbconn->username = "root";
 * $dbconn->password = "******";
 * $dbconn->connect();
 */
$dbconn = new DBConn( "test" );
if( !
$dbconn->connect() )
    exit(
"Connection to database could not be done! Please check, that DBConn class is properly setted." );
/**
 * Example 1.
 *
 * Create a new object and init with known id.
 */
$obj1 = new PhpDtObject( $dbconn );
$obj1->init( 1 );
print
"<pre>EXAMPLE 1.<br />";
print_r( $obj1->getIndex() );
print
"</pre>";
/**
 * Example 2.
 *
 * Create object with getOne method and parameter that specifies search criteria
 */
$obj2 = new PhpDtObject( $dbconn );
$params['where'] = "name = 'Bobby'";
$person = $obj2->getOne( $params );
$person->setValue( "age", 33 );
print
"<pre>EXAMPLE 2.<br />";
print
"<br>" . $person->getValue( "id" ) . "<br>";
print
"<br>" . $person->getValue( "name" ) . "<br>";
print
"<br>" . $person->getValue( "age" ) . "<br>";
print
"<br>" . $person->getValue( "date" ) . "<br>";
print
"</pre>";
/**
 * Save modified object to the database.
 *
 * You can make detailed inserts and updates with optional parameters:
 * $params = array( "where"=>"`column1` = 'value1' AND `column2` = 'value2'", "limit"=>"1" );
 * or
 * $params['custom_query'] = "UPDATE table1, table2 SET field = 'value' WHERE id > 1000";
 * $result = $obj2->save( $params );
 */
$person->save();
/**
 * Example 3.
 *
 * Make a new row to the table.
 */
$obj3 = new PhpDtObject( $dbconn );
//$obj3->setValue( "id", 0 );
$obj3->setValue( "name", "myName" );
$obj3->setValue( "age", 16 );
$obj3->setValue( "date", "2004-06-06" );
$obj3->save();
print
"<pre>EXAMPLE 3.<br />Saving...</pre>";
/**
 * Example 4.
 *
 * Delete the row from the database table.
 *
 * Change $id number to correspond real table row id number. Delete operation is commented for secure case. Uncomment
 * row 109 if you want example 4 to work.
 *
 * You can make detailed deletes with optional parameters:
 * $params['custom_query'] = "DELETE FROM table1, table2 WHERE id < 100 LIMIT 25";
 * $result = $obj4->delete( $params );
 */
$obj4 = new PhpDtObject( $dbconn );
$obj4->init( 4 );
//$obj4->delete();
print "<pre>EXAMPLE 4.<br />Deleting...</pre>";
/**
 * Example 5.
 *
 * Get all rows from the database table.
 *
 * You can make detailed searches with optional parameters:
 * $params = array( "select"=>"*", "where"=>"`id` < '10'", "limit"=>"-1", "order_by"=>"id", "order"=>"DESC" );
 * or $params['custom_query'] = "SELECT * FROM table1, table2 WHERE table1.id = table2.id";
 * and then:
 * $objects_array = $obj3->getMany( $params );
 */
$obj5 = new PhpDtObject( $dbconn );
$objects_array = $obj5->getMany();
print
"<pre>EXAMPLE 5.<br />";
foreach(
$objects_array as $num => $obj )
   
print_r( $obj->getIndex() );
print
"</pre>";
?>