Login   Register  
PHP Classes
elePHPant
Icontem

File: UsageExample.txt

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Roy Toledo  >  MySQL Wrapper Class 2  >  UsageExample.txt  >  Download  
File: UsageExample.txt
Role: Documentation
Content type: text/plain
Description: class.mysqldb Usage Example
Class: MySQL Wrapper Class 2
MySQL database access wrapper
Author: By
Last change:
Date: 2011-04-13 07:17
Size: 1,818 bytes
 

Contents

Class file image Download
Some Usage Examples 
________________________

First To Connect To A Mysql DB You Need To Set The Following Constants
	define('DBHost','~YOUR HOST~');
	define('DBUser','~YOUR USER NAME~');
	define('DBPassword','~YOUR PASSWORD~');
	define('DBDefaultDatabase','~Default DB~'); 
and create new object
	$db = new dbWrapper();

In order to work with multiple databases use 
	$db->set_db('otherDB');
	
SELECTS:
Select all columns in a table (*) [second argument optional]
	$db->selectTable('my_table', "firstname = 'john'");
Get one row query result as MYSQL_ASSOC
	$row = $db->queryRow($sql);	
Run Query
	$db->query($sql);
	Handle result:
	$db->getResult();
	Get All Returned Rows as a 3D Array
	$db->get3DArray();
	
Check if query returned results:
	$db->hasResults(); [returns false if empty, number of rows if full]
	
	
INSERT / UPDATE / REPLACE Usage
INSERT INTO my_table (firstname,lastname) VALUES ('john', 'doh');
will now be
$db->insert(array('firstname'=>'john', 'lastname'=>'doh'),'my_table');

Same syntax for replace and update, if your table uses a different Primary Key from "id" on update you need to specify your primary key as so
$db->update(array('firstname'=>'john', 'lastname'=>'doh'),'my_table', 'user_id');
	* Will Automaticly switch ' charecter with ` on string (can be set to use mysql_escape_string or anything else instead) 

Get Last Insert ID
	$db->getInsertId();

Transactions:
to begin a transaction use
	$db->begin();
following will commit or rollback if a failure had accured in the transaction (will return true for commit and false if rollback)
	$db->commit();

Backup Table - will backup a whole table and add the date to the new table name 
	$db->backupTable('my_table);
	
	
For easy debug turn debugging on 
	$db->debug = true;