<?php
/**
* @name objToSql class by Cale Orosz
* @author Cale Orosz
* @email elacdude@gmail.com
* @version 1.0
*
* You are free to use this code free of charge, modify it, and distrubute it,
* just leave this comment block at the top of this file.
*
* Go to phpclasses.org for examples on how to use this class
*
* Changes/Modifications
* 03/09/1985 - version 1.0 released
*
*/
require_once 'objtosql.class.php';
//your standard mysql connect info goes here
$sql_link = mysql_connect('localhost', 'testuser', 'password');
mysql_select_db('database');
//how to add values from GET or POST into the object
//normally these values would come from a $_POST or $_GET variable that has been populated by a <form> but i'm going to manually input the values here for example:
$_POST['contact_first_name'] = "cale";
$_POST['contact_last_name'] = "orosz";
$_POST['contact_address'] = "123 main st";
$_POST['contact_city'] = "Cleveland";
$_POST['contact_state'] = "Ohio";
$_POST['contact_zip'] = "44126";
$objtosql = new objToSql("contacts"); //name of the table
$objtosql->getValuesFrom($_POST, "contact_"); //loads any fields from the $_POST array with a prefix of 'contact_'
echo $objtosql->getInsertSql(); //returns: INSERT INTO contacts (`first_name`, `last_name`, `address`, `city`, `state`, `zip`) VALUES ('cale', 'orosz', '123 main st', 'Cleveland', 'Ohio', '44126')
echo "<br/>";
echo $objtosql->getUpdateSql(); //returns: UPDATE `contacts` set `first_name`='cale', `last_name`='orosz', `address`='123 main st', `city`='Cleveland', `state`='Ohio', `zip`='44126' WHERE `id`='55'
echo "<hr/>";
//generate an insert statement
$sqlgen = new objToSql("contacts");
$sqlgen->id = 1;
$sqlgen->first_name = 'cale';
$sqlgen->last_name = 'orosz';
$sqlgen->address = '123 main st';
$sqlgen->city = 'Cleveland';
$sqlgen->state = 'Ohio';
$sqlgen->zip = '44126';
echo $sqlgen->getInsertSql(); //returns: INSERT INTO contacts (`first_name`, `last_name`, `address`, `city`, `state`, `zip`) VALUES ('cale', 'orosz', '123 main st', 'Cleveland', 'Ohio', '44126')
echo "<hr/>";
$sqlgen->Insert(); //this will actually execute the query using mysql_query()
//generate an update statement
$sqlgen = new objToSql("contacts");
$sqlgen->first_name = 'cale';
$sqlgen->last_name = 'orosz';
$sqlgen->address = '123 main st';
$sqlgen->city = 'Cleveland';
$sqlgen->state = 'Ohio';
$sqlgen->zip = '44126';
$sqlgen->__where['id'] = '1';
echo $sqlgen->getUpdateSql(); //returns: UPDATE `contacts` set `first_name`='cale', `last_name`='orosz', `address`='123 main st', `city`='Cleveland', `state`='Ohio', `zip`='44126' WHERE `id`='55'
echo "<hr/>";
$sqlgen->Update(); //this will actually execute the query using mysql_query()
?>
|