PHP Classes

File: example1.php

Recommend this page to a friend!
  Classes of Carsten Gehling  >  sql_Generator  >  example1.php  >  Download  
File: example1.php
Role: Example script
Content type: text/plain
Description: Example on how to use the class
Class: sql_Generator
Class used to ease generation of SQL queries
Author: By
Last change:
Date: 19 years ago
Size: 2,246 bytes


Class file image Download
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)

// Including the sql_Generator class

// 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", "");
$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();
$sql = $sqlgen->makeUpdateKey("cust_id", $customer_id);


$con = mysql_connect("localhost", "root", "pollew32");
mysql_query($sql) or die(mysql_error());

if (
$action == "insert")
$cust_id = mysql_insert_id();

"Inserted id: $cust_id";


For more information send a message to info at phpclasses dot org.