PHP Classes

File: example.mquery.php

Recommend this page to a friend!
  Classes of Madis   MySQL Query Generator   example.mquery.php   Download  
File: example.mquery.php
Role: Example script
Content type: text/plain
Description: Example script
Class: MySQL Query Generator
Compose MySQL queries dynamically
Author: By
Last change: added example: using setkey() with array
Date: 19 years ago
Size: 1,274 bytes
 

Contents

Class file image Download
<?php

require_once('mquery.class.php');

// generate query for table people,
// use conditions for username and haircolor
// order by realname

$q=new mquery('people','username like "a%",haircolor="blue"','realname');

// generate a delete query based on defined conditions
echo "1: ".$q->genquery('delete')."<br>\n";

// OUTPUT:
// 1: DELETE FROM people WHERE ((username like "a%") AND (haircolor="blue"))

// drop haircolor condition
$q->unsetcondition('haircolor');

// add new evaluations
$q->setkey('shoesize','36');
$q->setkey(array(
   
'haircolor'=>'yellow',
   
'eyecolor'=>'blue'));

// generate an insert query
echo "2: ".$q->genquery('insert')."<br>\n";

// OUTPUT:
// 2: INSERT INTO people SET shoesize='36', haircolor='yellow', eyecolor='blue'

// add new haircolor condition
$q->setcondition('haircolor=10');

// drop eyecolor evaluation
$q->unsetkey('eyecolor');

// generate an update query
echo "3: ".$q->genquery('update')."<br>\n";

// OUTPUT:
// 3: UPDATE people SET shoesize='36', haircolor='yellow' WHERE ((username like "a%") AND (haircolor=10))

// generate a select query
echo "4: ".$q->genquery('select')."<br>\n";

// OUTPUT:
// 4: SELECT shoesize, haircolor FROM people WHERE ((username like "a%") AND (haircolor=10)) ORDER BY realname

?>