PHP Classes
Icontem

File: example.php


  Search   All class groups All class groups   Latest entries Latest entries   Top 10 charts Top 10 charts   Newsletter Newsletter   Blog Blog   Forums Forums   Help FAQ Help FAQ  
  Login   Register  
Recommend this page to a friend! ReTweet ReTweet Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of John Nunemaker  >  MySQL Table Wrapper  >  example.php  
File: example.php
Role: Example script
Content type: text/plain
Description: An example file which shows how to use the various class methods.
Class: MySQL Table Wrapper
Access rows of a MySQL table as objects
 

Contents

Class file image Download
<?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->price2) . ')' '</li>';
    }
    echo 
'</ul>';
}

// delete a record
$product->destroy(4);
?>

 
  Advertise on this site Advertise on this site   Site map Site map   Statistics Statistics   Site tips Site tips   Privacy policy Privacy policy   Contact Contact  

For more information send a message to :
info at phpclasses dot org.
Copyright (c) Icontem 1999-2009 PHP Classes - PHP Class Scripts
  PHP Book Reviews - Reviews of books and other products