README file for class.DB.php
Author: Amir Khawaja <amir@gorebels.net>
Date created: June 07, 2002.
Version: 1.3.1
This file contains the example usage for the class. It used to be in the header
of the class but that just increased the filesize a whole lot more than
needed. Also, please note, that unlike the other methods in this class,
the get and set type methods follow the pattern get_* and set_*.
Changelog:
Version June 28, 2002.
- Modified the disconnect() method to no longer check for an
open connection. Could cause some issues if the connection
is not persistent.
Version June 20, 2002.
- Altered constructor to allow backward compatibility with older version of
the class. The constructor now no longer checks for database
transaction support but the connect function will now handle that duty.
Version June 7, 2002.
- Updated the query function to support transaction.
+ New method (serverHasTransaction) to automatically check if the server
being connected to supports either the BDB, InnoDB, or Gemini table
type. If so, transaction feature in the class will be enabled.
+ New transaction methods: beginTransaction, commitTransaction,
rollbackTransaction.
+ New error method: get_errors. This to easily retrieve all
the error messages as an array.
+ Changed the constructor to allow specifying a MySQL host (optional).
+ Cleaned up the formatting of statements in each method.
Version 1.0.1a -- August 08, 2001
- updates contributed by MoMad: big_mo_mine@yahoo.com
+ Added new navigational features:
(moveNext, movePrev, moveFirst, and moveLast methods).
Version 1.0.1 -- June 05, 2001 (Date created)
+ Added new method "int fetchLastInsertId()"
+ Minor bug fixes
Class Methods:
- string get_dbhost()
- string get_dblogin()
- string get_dbpass()
- string get_dbname()
- array get_errors()
- void set_dbhost(string $value)
- void set_dblogin(string $value)
- void set_dbpass(string $value)
- void set_dbname(string $value)
- constructor void DB(string $dblogin, string $dbpass, string $dbname [, string $dbhost])
- int connect()
- void disconnect()
- string return_error(string $message)
- void showErrors()
- boolean hasErrors()
- void resetErrors()
- int query($sql)
- array fetchRow()
- array moveFirst()
- array moveLast()
- array moveNext()
- array movePrev()
- int fetchLastInsertId()
- int resultCount()
- boolean resultExist()
- void clear(int $result = 0)
- boolean serverHasTransactions()
- void beginTransaction()
- void commitTransaction()
- void rollbackTransaction()
Example usage of this class.
<?php
error_reporting(E_ALL);
require_once "class.DB.php";
$db = new DB("login", "pass", "dbname");
$db->query("SELECT fullname FROM table LIMIT 10");
/* loop through several rows of data */
while ($db->fetchRow()) {
echo $db->record["fullname"]."<br>\n";
}
/* this will echo "10" as the result count */
echo $db->resultCount()."<br>\n";
/* example of retrieving only one record and displaying it */
$db->query("SELECT fullname FROM table LIMIT 1");
$db->fetchRow();
echo $db->record["fullname"]."<br>\n";
/* this will echo "1" as the result count */
echo $db->resultCount()."<br>\n";
/* if there were any records from the previous SELECT query, then "WOOHOO" will be printed */
if ($db->resultExist()) {
echo "WOOHOO";
}
/* optional clearing of result set before issuing a "SELECT" statement */
$db->clear();
/* Begin a transaction, INSERT a new record and get the last inserted id */
$db->beginTransaction();
$db->query("INSERT INTO table1 SET user = 'am', password = 'am2'");
echo "<br>".$db->fetchLastInsertId()."<br><br>\n";
$db->commitTransaction();
/* diconnect and show errors */
$db->disconnect();
$db->showErrors();
?>
Example using the move* methods. Generously Provided my MoMad <big_mo_mine@yahoo.com>.
<?php
// test.php
require_once "DB.class.php";
header("content-type: text/html");
print "<html><head><title>HAHA</title></head><body>";
print "<h1>HELLO WORLD DB VERSION</h1><br>\n\n";
$db = new DB("login", "pass", "dbname");
// table: test
// fields: id, title, price
$db->query("SELECT * FROM test");
print "<table bgcolor=\"#FFFFCC\" border=1><tr><th>id</th><th>title</th><th>price</th></tr>\n";
while( $db->fetchRow() )
{
print "<tr><td>" . $db->record["id"] . "</td><td>" . $db->record["title"] .
"</td><td>" . $db->record["price"] . "</td></tr>\n";
}
print "</table>\n\n";
print "<p>TESTING moveFirst()</p>\n";
if( $db->moveFirst() )
{
print "FIRST REC: [" . $db->record["id"] . " || " . $db->record["title"] .
" || " . $db->record["price"] . "]<br><br>\n";
}
print "<p>TESTING moveNext()</p>\n";
if( $db->moveNext() )
{
print "NEXT REC: [" . $db->record["id"] . " || " . $db->record["title"] .
" || " . $db->record["price"] . "]<br><br>\n";
}
print "<p>TESTING moveLast()</p>\n";
if( $db->moveLast() )
{
print "LAST REC: [" . $db->record["id"] . " || " . $db->record["title"] .
" || " . $db->record["price"] . "]<br><br>\n";
}
print "<p>TESTING movePrev()</p>\n";
if( $db->movePrev() )
{
print "PREV REC: [" . $db->record["id"] . " || " . $db->record["title"] .
" || " . $db->record["price"] . "]<br><br>\n";
}
// move to the last record and move backwords
$db->moveLast();
print "<p>BACKWORDS:</p>\n";
print "<table bgcolor=\"#FFFFCC\" border=1><tr><th>id</th><th>title</th><th>price</th></tr>\n";
while( $db->movePrev() )
{
print "<tr><td>" . $db->record["id"] . "</td><td>" . $db->record["title"] .
"</td><td>" . $db->record["price"] . "</td></tr>\n";
}
print "</table>\n\n";
// disconnect and show errors
$db->disconnect();
$db->showErrors();
print "</body></html>";
?>
|