<?php
 
 
/**
 
A full article on how to use this class can be found on my website:
 
http://addictedtonew.com/archives/82/php-and-mysql-table-wrapper/
 
 
 
The SQL to help with the tests below.
 
-- 
 
-- Table structure for table `products`
 
-- 
 
CREATE TABLE `products` (
 
  `id` int(10) unsigned NOT NULL auto_increment,
 
  `title` varchar(100) NOT NULL default '',
 
  `description` text NOT NULL,
 
  `price` decimal(6,2) NOT NULL default '0.00',
 
  `date_created` int(11) NOT NULL default '0',
 
  PRIMARY KEY  (`id`)
 
) ENGINE=MyISAM DEFAULT CHARSET=latin1 PACK_KEYS=0 AUTO_INCREMENT=4 ;
 
 
-- 
 
-- Dumping data for table `products`
 
-- 
 
INSERT INTO `products` VALUES (1, 'Hair Spray', 'A New Shiny Description', 9.95, 1124249200);
 
INSERT INTO `products` VALUES (2, 'Speakers', 'A New Shiny Description', 1.99, 1124250303);
 
INSERT INTO `products` VALUES (3, 'Funky Gel', 'Great for styling your hair', 7.99, 1124765799);
 
        
 
*/
 
 
require_once 'class.mysql.php';
 
require_once 'class.table.php';
 
 
// just put the host, username, password and database in place below
 
$db =& new MySQL('localhost', 'root', '', 'class_test');
 
if ($db->isError()) {
 
    echo $db->getErrorMsg();
 
}
 
 
// example use of getOne() method
 
echo 'Row Count: ' . $db->getOne("SELECT COUNT(*) FROM products") . '<br />';
 
if ($db->isError()) {
 
    echo $db->getErrorMsg();
 
}
 
 
//-----------------------------------------------------------------------
 
// create a new table wrapper
 
$product =& new table($db, 'products');
 
 
// insert a product
 
$product->title         = 'Shoes';
 
$product->description     = 'Red with tan stripes';
 
$product->price         = 29.95;
 
$product->date_created     = time();
 
$product->save();
 
 
// update a product
 
$product->find(1);
 
$product->title = 'New Hair Spray';
 
$product->price = 19.95;
 
$product->save();
 
 
// or update a product like this<br>
 
$product->find(1);
 
$product->updateAttr(array('price'=>15.95));
 
 
// or even like this
 
$product->updateAttr(array('price'=>12.95), 1);
 
 
// example of how to find many records
 
$by_title = $product->findMany('WHERE price > 6', 'ORDER BY price DESC');
 
if ($by_title) {
 
    echo '<ul>';
 
    foreach($by_title as $p) {
 
        echo '<li>' . $p->id . ': ' . $p->title . ' ($' . number_format($p->price, 2) . ')' . '</li>';
 
    }
 
    echo '</ul>';
 
}
 
 
// delete a record
 
$product->destroy(4);
 
?>
 
 |