PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of herve_tribouilloy   DBConnect   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: example to use connection.php file
Class: DBConnect
MySQL database access wrapper
Author: By
Last change: The example page shows all the possibility of the class
Date: 19 years ago
Size: 3,438 bytes
 

Contents

Class file image Download
<?php
include('class/connection.php');

function
display($string,$title = false) {
    if (
$title)
        echo
"<br /><b>$string</b><br />";
    else
        echo
"$string<br />";
}

$oTest = new DBConnect("test1");

/*delete query */
display("delete all the records",1);

$oTest->setQuery('delete from test_table');
if (!
$oTest->hasError())
   
display("table test_table is empty");
else
   
display("error. table test_table couldn't be emptied");

$oTest->clearError();

/*new record */
display("new record",1);

$description = "desc test";
$test_date ="2004-04-02";

$lastId = $oTest->setQuery("insert into test_table (test_date,description) values ('%s','%s')",
$test_date,$description);
if (
$lastId > 1)
   
display("record created. the id is ".$lastId);
else
   
display("error. Record not created");

$oTest->clearError();

/*wrong insert query: throwing an error without crashing and diying */
display("wrong insert query: throwing an error without crashing and diying",1);

$oTest->setQuery('insert dummyfield from test_table');

if (
$oTest->hasError()) {
   
display($oTest->getError());
}
else
   
display("no mistake");

$oTest->clearError();

/*updating a record: no validation, supposed to be within another class, here the SQL must be ok */
display("updating record",1);

$description = "updated desc test";
$test_date ="2000-04-06";
$test_id = $lastId;

$oTest->setQuery("update test_table set test_date='%s', description='%s' where test_id=%d",$test_date,$description,$test_id);
if (!
$oTest->hasError())
   
display("record $test_id updated");
else
   
display("error. Record $test_id not updated");

$oTest->clearError();

/*select query */
display("select record",1);

$data = $oTest->getRecord("select test_id, test_date,description from test_table where test_id=%d",$test_id);

echo
"id ".$data->test_id."<br />";
echo
"date ".$data->test_date."<br />";
echo
"description ".$data->description."<br />";

/* create another record */
display("create another record",1);

$description = "desc of another record";
$test_date ="2000-10-01";

$oTest = new DBConnect("test1"); // optional, the MySQL connection will be reused
$lastId = $oTest->setQuery("insert into test_table (test_date,description) values ('%s','%s')",
$test_date,$description);
if (
$lastId > 1)
   
display("record created. the id is ".$lastId);
else
   
display("error. Record not created.");

$oTest->clearError();

/*select a list of records */
display("select list of record",1);

$result = $oTest->getResult("select test_id, test_date,description from test_table");

$i = 1;
foreach (
$result as $data) {
   
display("select $i eme record of the list",1);
   
display("id ".$data->test_id);
   
display("date ".$data->test_date);
   
display("description ".$data->description);
   
$i++;
}

$oTest->clearError();

/*wrong select query: throwing an error without crashing and diying */
display("wrong select query: throwing an error without crashing and diying",1);

$oTest->getRecord();

if (
$oTest->hasError()) {
   
display($oTest->getError());
}
else
   
display("no mistake");

$oTest->clearError();

/*delete query */
display("delete a record",1);

$oTest->setQuery('delete from test_table where test_id=%d',$data->test_id);
if (!
$oTest->hasError())
   
display("record {$data->test_id} deleted");
else
   
display("error. Record {$data->test_id} not deleted");

$oTest->clearError();

?>