I'm using PHP SQL PARSER
my Code
<?php
require_once dirname(__FILE__) . '/../src/PHPSQLParser.php';
$sql = 'SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID=Customers.CustomerID where
Customers.CustomerName = "Siddhu"';
$sql = strtolower($sql);
echo $sql . "\n";
$parser = new PHPSQLParser($sql, true);
echo "<pre>";
print_r($parser->parsed);
?>
I'm getting output like below array
Array (
[SELECT] => Array
(
[0] => Array
(
[expr_type] => colref
[alias] =>
[base_expr] => orders.orderid
[no_quotes] => orders.orderid
[sub_tree] =>
[delim] => ,
[position] => 7
)
[1] => Array
(
[expr_type] => colref
[alias] =>
[base_expr] => customers.customername
[no_quotes] => customers.customername
[sub_tree] =>
[delim] => ,
[position] => 23
)
[2] => Array
(
[expr_type] => colref
[alias] =>
[base_expr] => orders.orderdate
[no_quotes] => orders.orderdate
[sub_tree] =>
[delim] =>
[position] => 47
)
)
[FROM] => Array
(
[0] => Array
(
[expr_type] => table
[table] => orders
[no_quotes] => orders
[alias] =>
[join_type] => JOIN
[ref_type] =>
[ref_clause] =>
[base_expr] => orders
[sub_tree] =>
[position] => 70
)
[1] => Array
(
[expr_type] => table
[table] => customers
[no_quotes] => customers
[alias] =>
[join_type] => LEFT
[ref_type] => ON
[ref_clause] => Array
(
[0] => Array
(
[expr_type] => colref
[base_expr] => orders.customerid
[no_quotes] => orders.customerid
[sub_tree] =>
[position] => 101
)
[1] => Array
(
[expr_type] => operator
[base_expr] => =
[sub_tree] =>
[position] => 118
)
[2] => Array
(
[expr_type] => colref
[base_expr] => customers.customerid
[no_quotes] => customers.customerid
[sub_tree] =>
[position] => 119
)
)
[base_expr] => customers on orders.customerid=customers.customerid
[sub_tree] =>
[position] => 88
)
)
[WHERE] => Array
(
[0] => Array
(
[expr_type] => colref
[base_expr] => customers.customername
[no_quotes] => customers.customername
[sub_tree] =>
[position] => 146
)
[1] => Array
(
[expr_type] => operator
[base_expr] => =
[sub_tree] =>
[position] => 169
)
[2] => Array
(
[expr_type] => const
[base_expr] => "siddhu"
[sub_tree] =>
[position] => 171
)
)
)
Now I want to generate the query using this array. Why am I doing this, later I will add additional parameters to this array. like I pass additional condition in WHERE clause or Table
FOR EXAMPLE: Previous query
$sql = 'SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID=Customers.CustomerID where
Customers.CustomerName = "Siddhu"';
Now I want to pass two more conditions in where clause like after WHERE condition Customers.CustomerID = "123" and status = "Active" and created_by = 1;
so here my final query is like
$sql = 'SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
LEFT JOIN Customers ON Orders.CustomerID=Customers.CustomerID where
Customers.CustomerName = "Siddhu" AND Customers.CustomerID = "123" and status = "Active" and created_by = 1;
So how can I achieve it, or Is there any function in PHPSQLPARSER using this array to have any function to generate the query? Thank you for Advance and Sorry for any grammar mistakes