<?php
/********************************************************************
Example file
This example shows how to use the sql_Generator class
The example is based on the following MySQL table:
CREATE TABLE customer (
cust_id int(10) unsigned NOT NULL auto_increment,
name varchar(60) NOT NULL default '',
email varchar(60) NOT NULL default '',
age tinyint(3) unsigned NOT NULL default '0',
date_created datetime NOT NULL default '0000-00-00 00:00:00',
date_modified datetime NOT NULL default '0000-00-00 00:00:00',
PRIMARY KEY (cust_id)
) TYPE=MyISAM;
********************************************************************/
///////////////////////////////////////////////
// Including the sql_Generator class
require_once("class_sql_generator.php");
///////////////////////////////////////////////
// This code shows how to insert a new record
$action = "insert";
///////////////////////////////////////////////
// Use this code instead, if you want an example of updating a record
// $action = "update";
// $customer_id = 1;
///////////////////////////////////////////////
// Create new generator class
$sqlgen = new sql_Generator("customer");
///////////////////////////////////////////////
// Add common fields
$sqlgen->addField("name", "Carsten Gehling");
$sqlgen->addField("email", "carsten@sarum.dk");
$sqlgen->addField("age", 31);
///////////////////////////////////////////////
// The field "date_modified" should be updated using a MySQL function
$sqlgen->addField("date_modified", "now()", "function");
///////////////////////////////////////////////
// We only want to fill the field "date_created" if this is a new record
if ($action == "insert")
$sqlgen->addField("date_created", "now()", "function");
///////////////////////////////////////////////
// Generate the sql statement
if ($action == "insert")
$sql = $sqlgen->makeInsert();
else
$sql = $sqlgen->makeUpdateKey("cust_id", $customer_id);
echo $sql;
$con = mysql_connect("localhost", "root", "pollew32");
mysql_select_db("test");
mysql_query($sql) or die(mysql_error());
if ($action == "insert")
$cust_id = mysql_insert_id();
echo "Inserted id: $cust_id";
mysql_close();
?>
|