note 76759 added to function.mysql-field-table
| From: | jorge at rhst dot net | Date: | Sun, 29 Jul 2007 21:57:47 +0000 |
| Subject: | note 76759 added to function.mysql-field-table | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-129028@lists.php.net to get a copy of this message | ||
The function below takes a function and returns the col->table mapping as an array.
For example:
$query = âSELECT a.id AS a_id, b.id b_id FROM atable AS a, btable bâ
$cols = queryAlias($query);
print_r($cols);
Returns:
Array
(
[a] => atable
[b] => btable
)
I can't promise it's perfect, but this function never hit production cause I ended up
using mysqli methods instead.
Enjoy
-Jorge
/**
* Takes in a query and returns the alias->table mapping.
*
* @param string $query
* @return array of alias mapping
*/
function queryAlias ( $query ) {
//Make it all lower, we ignore case
$substr = strtolower($query);
//Remove any subselects
$substr = preg_replace ( â/\(.*\)/â, â, $substr);
//Remove any special charactors
$substr = preg_replace ( â/[^a-zA-Z0-9_,]/â, â â, $substr);
//Remove any white space
$substr = preg_replace(â/\s\s+/â, â â, $substr);
//Get everything after FROM
$substr = strtolower(substr($substr, strpos(strtolower($substr),â from â) + 6));
//Rid of any extra commands
$substr = preg_replace(
Array(
â/ where .*+$/â,
â/ group by .*+$/â,
â/ limit .*+$/â ,
â/ having .*+$/â ,
â/ order by .*+$/â,
â/ into .*+$/â
), â, $substr);
//Remove any JOIN modifiers
$substr = preg_replace(
Array(
â/ left /â,
â/ right /â,
â/ inner /â,
â/ cross /â,
â/ outer /â,
â/ natural /â,
â/ as /â
), â â, $substr);
//Replace JOIN statements with commas
$substr = preg_replace(Array(â/ join /â, â/ straight_join /â),
â,â, $substr);
$out_array = Array();
//Split by FROM statements
$st_array = split (â,â, $substr);
foreach ($st_array as $col) {
$col = preg_replace(Array(â/ on .*+/â), â, $col);
$tmp_array = split(â â, trim($col));
//Oh no, something is wrong, letâs just continue
if (!isset($tmp_array[0]))
continue;
$first = $tmp_array[0];
//If the âASâ is set, lets include that, if not, well, guess this table isnât
aliased.
if (isset($tmp_array[1]))
$second = $tmp_array[1];
else
$second = $first;
if (strlen($first))
$out_array[$second] = $first;
}
return $out_array;
}
----
Server IP: 69.147.83.197
Probable Submitter: 76.98.60.15
----
Manual Page -- http://www.php.net/manual/en/function.mysql-field-table.php
Edit -- https://master.php.net/note/edit/76759
Del: integrated -- https://master.php.net/note/delete/76759/integrated
Del: useless -- https://master.php.net/note/delete/76759/useless
Del: bad code -- https://master.php.net/note/delete/76759/bad+code
Del: spam -- https://master.php.net/note/delete/76759/spam
Del: non-english -- https://master.php.net/note/delete/76759/non-english
Del: in docs -- https://master.php.net/note/delete/76759/in+docs
Del: other reasons-- https://master.php.net/note/delete/76759
Reject -- https://master.php.net/note/reject/76759
Search -- https://master.php.net/manage/user-notes.php