This class can build a MySQL query with an array of parameters.
This class can create SELECT, INSERT, UPDATE, and DELETE statements from the specified table and field parameters.
This class can create a where condition for an SQL statement, like a query statement that uses a LEFT join and an order statement.
Example:
? Php
/* *******************************************************************
Example file
This example shows the Mylibsqlgen class
The example is based on the following MySQL table:
CREATE TABLE Customer (
ID Int (a) unsigned not NULL auto_increment,
Name varchar not NULL default ',
Address varchar is not NULL default ',
City varchar is not NULL default ',
PRIMARY KEY (cust_id)
) Type=myisam;
******************************************************************* */
Require_once ("class_mylib_sqlgen-1.0.php");
$fields = Array ("name", "Address", "City");
$values = Array ("Fadjar", "Resultmang Raya Street", "Jakarta");
$tables = Array ("Customer");
echo "<b>result Generate insert</b><br>";
$object = new Mylibsqlgen ();
$object-> clear_all_assign (); To the refresh all property but it no need when
$object-> Setfields ($fields);
$object-> setvalues ($values);
$object-> settables ($tables);
if (! $object-> Getinsertsql ()) {echo $object-> Error; exit;}
else {$sql = $object-> result; Echo $sql. "<br>";}
echo "<b>result Generate update</b><br>";
$fields = Array ("name", "Address", "City");
$values = Array ("Fadjar", "Resultmang Raya Street", "Jakarta");
$tables = Array ("Customer");
$id = 1;
$conditions [0] ["condition"] = "id= ' $id '";
$conditions [0] ["connection"] = "";
$object-> clear_all_assign ();
$object-> Setfields ($fields);
$object-> setvalues ($values);
$object-> settables ($tables);
$object-> setconditions ($conditions);
if (! $object-> Getupdatesql ()) {echo $object-> Error; exit;}
else {$sql = $object-> result; Echo $sql. "<br>";}
echo "<b>result Generate delete</b><br>";
$tables = Array ("Customer");
$conditions [0] ["condition"] = "id= ' 1 '";
$conditions [0] ["connection"] = "or";
$conditions [1] ["condition"] = "id= ' 2 '";
$conditions [1] ["connection"] = "or";
$conditions [2] ["condition"] = "id= ' 4 '";
$conditions [2] ["connection"] = "";
$object-> clear_all_assign ();
$object-> settables ($tables);
$object-> setconditions ($conditions);
if (! $object-> Getdeletesql ()) {echo $object-> Error; exit;}
else {$sql = $object-> result; Echo $sql. "<br>";}
echo "<b>result Generate list</b><br>";
$fields = Array ("id", "name", "Address", "City");
$tables = Array ("Customer");
$id = 1;
$conditions [0] ["condition"] = "id= ' $id '";
$conditions [0] ["connection"] = "";
$object-> clear_all_assign ();
$object-> Setfields ($fields);
$object-> settables ($tables);
$object-> setconditions ($conditions);
if (! $object-> Getquerysql ()) {echo $object-> Error; exit;}
else {$sql = $object-> result; Echo $sql. "<br>";}
echo "<b>result Generate List with search in all fields</b><br>";
$fields = Array ("id", "name", "Address", "City");
$tables = Array ("Customer");
$id = 1;
$search = "Fadjar Nurswanto";
$object-> clear_all_assign ();
$object-> Setfields ($fields);
$object-> settables ($tables);
$object-> Setsearch ($search);
if (! $object-> Getquerysql ()) {echo $object-> Error; exit;}
else {$sql = $object-> result; Echo $sql. "<br>";}
echo "<b>result Generate List with search on some fields</b><br>";
$fields = Array ("id", "name", "Address", "City");
$tables = Array ("Customer");
$id = 1;
$search = Array (
"Name" => "Fadjar Nurswanto",
"Address" => "Tomang Raya"
);
$object-> clear_all_assign ();
$object-> Setfields ($fields);
$object-> settables ($tables);
$object-> Setsearch ($search);
if (! $object-> Getquerysql ()) {echo $object-> Error; exit;}
else {$sql = $object-> result; Echo $sql. "<br>";}
?>