note 45115 deleted from function.explode by aidan
| From: | aidan@php.net | Date: | Thu, 26 Aug 2004 22:39:37 +0000 |
| Subject: | note 45115 deleted from function.explode by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75378@lists.php.net to get a copy of this message | ||
Note Submitter: fms at hy dot com dot br
----
This function could be usefull if you need to parse a "SELECT" command into blocks with
(select, from, where, group by, order by) of data.
function parseSELECT(&$q) {
/*****************
by FMS & XANDE on Ago/2004
$q contains a SELECT comand. Example:
SELECT row_id,fullname as name,age FROM tabletest WHERE row_id<10 GROUP BY row_id ORDER BY
age,name
*****************/
// ensures that all keywords are in lowercase
$q=eregi_replace("select" ,"select" ,$q);
$q=eregi_replace("from" ,"from" ,$q);
$q=eregi_replace("where" ,"where" ,$q);
$q=eregi_replace("group by","group by",$q);
$q=eregi_replace("order by","order by",$q);
// does the job
$orderby=explode("order by",$q);
$groupby=explode("group by",$orderby[0]);
$orderby=$orderby[1];
$where =explode("where" ,$groupby[0]);
$groupby=$groupby[1];
$from =explode("from" ,$where[0]);
$where =$where[1];
$select =explode("select" ,$from[0]);
$from =$from[1];
$select =$select[1];
// prepares an array to return
$rt['select' ]=$select;
$rt['from']=$from;
$rt['where']=$where;
$rt['groupby']=$groupby;
$rt['orderby']=$orderby;
return $rt;
}