PHP Classes

File: examples.php

Recommend this page to a friend!
  Classes of Andrei Caminschi   MySQLQueryGenerator   examples.php   Download  
File: examples.php
Role: Example script
Content type: text/plain
Description: Examples ...
Class: MySQLQueryGenerator
Compose common MySQL queries from parameters
Author: By
Last change:
Date: 15 years ago
Size: 4,674 bytes
 

Contents

Class file image Download
<?php

function pr($a)
{
    echo
'<pre>';
    echo
$a;
    echo
'</pre>';
}

pr('Please check the source code for each query. Here are displayed only the results.');
pr('Please email me on <b>zzerachiel@gmail.com</b> for any bugs or new features.');
pr('Thank you.');
include
'string.php';
include
'mysqlSelectQuery.php';
include
'mysqlDeleteQuery.php';
include
'mysqlUpdateQuery.php';
include
'mysqlInsertQuery.php';

pr('<b>INSERT Examples</b> <hr />');

pr('<b>1</b>) Simple insert example');

$insertQuery = new mysqlInsertQuery('users');
$insertQuery->set('username','someusername',true);
$insertQuery->set('password',md5('password'),true);
$insertQuery->set('createdOn','UNIX_TIMESTAMP()');
pr($insertQuery);

pr('<b>2</b>) Priority DELAYED');
$insertQuery = new mysqlInsertQuery('users');
$insertQuery->set('username','someusername',true);
$insertQuery->set('password',md5('password'),true);
$insertQuery->set('createdOn','UNIX_TIMESTAMP()');
$insertQuery->setPriority(Q_INSERT_PRIORTY_DELAYED);
pr($insertQuery);

pr('<b>3</b>) Priority LOW');
$insertQuery = new mysqlInsertQuery('users');
$insertQuery->set('username','someusername',true);
$insertQuery->set('password',md5('password'),true);
$insertQuery->set('createdOn','UNIX_TIMESTAMP()');
$insertQuery->setPriority(Q_INSERT_PRIORTY_LOW);
pr($insertQuery);

pr('<b>4</b>) Priority HIGH');
$insertQuery = new mysqlInsertQuery('users');
$insertQuery->set('username','someusername',true);
$insertQuery->set('password',md5('password'),true);
$insertQuery->set('createdOn','UNIX_TIMESTAMP()');
$insertQuery->setPriority(Q_INSERT_PRIORTY_HIGH);
pr($insertQuery);

pr('<b>5</b>) On Duplicate');
$insertQuery = new mysqlInsertQuery('users');
$insertQuery->set('username','someusername',true);
$insertQuery->set('password',md5('password'),true);
$insertQuery->set('createdOn','UNIX_TIMESTAMP()');
$insertQuery->onDuplicate('username','someusername1',true);
pr($insertQuery);

pr('<b>6</b>) :P Usefull');
/**
 * For demo purposes let's say we have this situation
 * */
$tableFieldsTypes = array( // 1 - trng, 0 - numeric
   
'username' =>1,
   
'password' =>1,
   
'createdOn' =>0
);
$dataToBeInserted = array(
   
'username' => 'zzerachiel',
   
'password' => md5('pass'),
   
'createdOn'=> 'UNIX_TIMETAMP'
);

$insertQuery = new mysqlInsertQuery('users');
foreach (
$dataToBeInserted as $col=>$value)
   
$insertQuery->set($col,$value,$tableFieldsTypes[$col]);
pr($insertQuery);


pr('<br />');
pr('<b>UPDATE Examples</b> <hr />');

pr("<b>1</b>) Basic example");
$updateQuery = new mysqlUpdateQuery('users');
$updateQuery->set('username','zzerachiel',true);
$updateQuery->where(array('id'=>1));
pr($updateQuery);

pr("<b>2</b>) A more complex exampe, using logical operators in where clause");
$updateQuery = new mysqlUpdateQuery('users');
$updateQuery->set('username','zzerachiel',true);
$whereConditions = array(
   
'id'=>1,
   
'and',
   
'createdOn < UNIX_TIMESTAMP()'
);
$updateQuery->where($whereConditions);
pr($updateQuery);

pr("<b>3</b>) Where, Order By &amp; Limit");
$updateQuery = new mysqlUpdateQuery('users');
$updateQuery->set('username','zzerachiel',true);
$whereConditions = array(
   
'id'=>1,
   
'and',
   
'createdOn < UNIX_TIMESTAMP()'
);
$updateQuery->where($whereConditions);
$updateQuery->orderBy('username');
$updateQuery->orderBy('id','DESC');
$updateQuery->limit(10);
pr($updateQuery);


pr('<br />');
pr('<b>DELETE Examples</b> <hr />');

pr("<b>1</b>) Basic example");
$deleteQuery = new mysqlDeleteQuery('users');
$whereConditions = array(
   
'id' => 1,
   
'and',
   
'createdOn > UNIX_TIMESTAMP()'
);
$deleteQuery->where($whereConditions);
pr($deleteQuery);

pr("<b>2</b>) Delete the first 10 records based on id field ");
$deleteQuery = new mysqlDeleteQuery('users');
$deleteQuery->orderBy('id','ASC');
$deleteQuery->limit(10);
pr($deleteQuery);

pr('<br />');
pr('<b>SELECT Examples</b> <hr />');

pr("<b>1</b>) Basic example");
$selectQuery = new mysqlSelectQuery;
$selectQuery->from('users');
$selectQuery->orderBy('id');
$selectQuery->select_expr('id','username');
pr($selectQuery);

pr("<b>2</b>) Multiple tables using aliases");
$selectQuery = new mysqlSelectQuery;
$selectQuery->from('users','u');
$selectQuery->from('preferences','p');
$selectQuery->select_expr('u.username','p.language');
pr($selectQuery);

pr("<b>3</b>) WHERE, GROUP and LIMIT");
$selectQuery = new mysqlSelectQuery;
$selectQuery->from('users');
$selectQuery->select_expr('*');
$selectQuery->where(array('id > 1'));
$selectQuery->groupBy('createdOn');
$selectQuery->limit(0,20);
pr($selectQuery);

?>