PHP Classes
Icontem

File: example.new_features.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 Jeff Williams  >  Ultimate MySQL  >  example.new_features.php  
File: example.new_features.php
Role: Example script
Content type: text/plain
Description: Example (SQL generation and try/catch blocks)
Class: Ultimate MySQL
MySQL database access wrapper
 

Contents

Class file image Download
<?php
// Include the Ultimate MySQL class and create the object
include("mysql.class.php");
$db = new MySQL();

// Connect to the database
// CHANGE THESE VALUES TO MATCH YOUR DATABASE!
if (! $db->Open(true"test""localhost""root""password")) $db->Kill();

// --------------------------------------------------------------------------
// Want to know if you are connected? Use IsConnected()
echo "Are we connected? ";
var_dump($db->IsConnected());
echo 
"\n<br />\n";

// --------------------------------------------------------------------------
// Now we can generate SQL statements from arrays!

// Let's create an array for the examples
// $arrayVariable["column name"] = formatted SQL value
$values["Name"] = MySQL::SQLValue("Violet");
$values["Age"]  = MySQL::SQLValue(777MySQL::SQLVALUE_NUMBER);

// Echo out some SQL statements
echo "<pre>" "\n";
echo 
MySQL::BuildSQLDelete("Test"$values) . "\n<br />\n";
echo 
MySQL::BuildSQLInsert("Test"$values) . "\n<br />\n";
echo 
MySQL::BuildSQLSelect("Test"$values) . "\n<br />\n";
echo 
MySQL::BuildSQLUpdate("Test"$values$values) . "\n<br />\n";
echo 
MySQL::BuildSQLWhereClause($values) . "\n<br />\n";
echo 
"</pre>" "\n";

// Or create more advanced SQL SELECT statements
$columns = array("Name""Age");
$sort "Name";
$limit 10;
echo 
MySQL::BuildSQLSelect("Test"$values$columns$sorttrue$limit);
echo 
"\n<br />\n";

$columns = array("Color Name" => "Name""Total Age" => "Age");
$sort = array("Age""Name");
$limit "10, 20";
echo 
MySQL::BuildSQLSelect("Test"$values$columns$sortfalse$limit);
echo 
"\n<br />\n";

// The following methods take the same parameters and automatically execute!

// $db->DeleteRows("Test", $values);
// $db->InsertRow("Test", $values);
// $db->SelectRows("Test", $values, $columns, $sort, true, $limit);
// $db->UpdateRows("Test", $values1, $values2);

// You can also select an entire table
// $db->SelectTable("Test");

// Or truncate and clear out an entire table
// $db->TruncateTable("Test");

// --------------------------------------------------------------------------

// Now you can throw exceptions and use try/catch blocks
$db->ThrowExceptions true;

try {
    
// This next line will always cause an error
    
$db->Query("BAD SQL QUERY TO CREATE AN ERROR");
catch(Exception $e) {
    
// If an error occurs, do this (great for transaction processing!)
    
echo "We caught the error: " $e->getMessage();
}

// Or let's show a stack trace if we do not use a try/catch
// This shows the stack and tells us exactly where it failed
$db->Query("BAD SQL QUERY TO CREATE AN ERROR");

?>

 
  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