File: example.MySQL.php

Recommend this page to a friend!
  Classes of Richard Munroe  >  dm.DB  >  example.MySQL.php  >  Download  
File: example.MySQL.php
Role: Example script
Content type: text/plain
Description: Example for MySQL
Class: dm.DB
Generic database access wrapper
Author: By
Last change:
Date: 15 years ago
Size: 2,012 bytes
 

Contents

Class file image Download
<?php

/**
 * @author Dick Munroe <munroe@csworks.com>
 * @copyright copyright @ 2006 by Dick Munroe, Cottage Software Works, Inc.
 * @license http://www.csworks.com/publications/ModifiedNetBSD.html
 * @version 1.0.0
 * @package dm.DB
 * @example ./example.php
 */

/*
 * This script assumes that the MySQL database created by DB.sql exists.
 */

error_reporting(E_ALL);

include_once
"class.factory.DB.php" ;

/*
 * Put the value of your database login name into this variable.
 */

$dblogin = "put your login name here" ;

/*
 * Put the value of your database login password into this variable.
 */

$dbpassword = "put your password here" ;

/*
 * Should you change the name of the database created by DB.sql, then
 * put the new name in the following variable.
 */

$dbdatabase = "DB" ;

$db = FactoryDB::factory($dblogin, $dbpassword, $dbdatabase);
$db->queryConstant("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->queryConstant("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->queryConstant("INSERT INTO `table` SET user = 'am', password = 'am2'");
echo
"<br>".$db->fetchLastInsertId()."<br><br>\n";
$db->commitTransaction();

/* diconnect and show errors */
$db->disconnect();
$db->showErrors();

?>

For more information send a message to info at phpclasses dot org.