I've been working with this query to achieve the results I wanted, which I have. During this time I started using PDO and at first worked by just having one varibale to prepare inside the query. While testing, I ended up using two more variables inside the query but left them out of the prepared statement until I was finished.
Now that I have the query completed, I went on and tried preparing the three variables as I did with the first one but an error was displayed when I ran the query:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '':cdrLastTable' ON a.
id
= b.`source_customer_billing_zone_' at line 2
I noticed the error was coming from one of the three prepared variables I needed for my query, but the thing is that the one that's failing is the only one placed before the WHERE
clause. Excluding it from the prepared statement works fine. After reading and looking at exaples I could not find any with a variable before the WHERE
clause so my question is:
Can you prepare variables before the WHERE
clause? If this is not possible, then what would be the point on using PDO
for this query?
I'm assuming that this issue does not relate to the query or any syntax error since removing the variable from the prepared statement just works, but I could be wrong as well.
This is the query with all variables prepared (the one that gives off the error):
$stmtOutgoing = $connAccounting->prepare("SELECT a.`zone` AS zone, init_time, a.`detail` AS sku, b.`source_external_subscriber_id`, count(*) AS llamadas, SUM((CEIL(b.`duration`))) AS duracion, SUM((b.`source_customer_cost`)/100) AS total, SUM((b.`source_customer_cost`)/100)/(SUM((CEIL(b.`duration`)))) AS precio
FROM `billing`.billing_zones_history a INNER JOIN :cdrLastTable
ON a.`id` = b.`source_customer_billing_zone_id`
WHERE source_external_subscriber_id = :source_external_subscriber_id AND init_time >= :queryLastTstmp
GROUP BY a.`detail`
ORDER BY total desc");
$stmtOutgoing->execute(array('source_external_subscriber_id' => $source_external_subscriber_id, 'cdrLastTable' => $cdrLastTable, 'queryLastTstmp' => $queryLastTstmp));
You can see the error message points directly at :cdrLastTable
right after the INNER JOIN
, and effectively removing it like this:
$stmtOutgoing = $connAccounting->prepare("SELECT a.`zone` AS zone, init_time, a.`detail` AS sku, b.`source_external_subscriber_id`, count(*) AS llamadas, SUM((CEIL(b.`duration`))) AS duracion, SUM((b.`source_customer_cost`)/100) AS total, SUM((b.`source_customer_cost`)/100)/(SUM((CEIL(b.`duration`)))) AS precio
FROM `billing`.billing_zones_history a INNER JOIN $cdrLastTable
ON a.`id` = b.`source_customer_billing_zone_id`
WHERE source_external_subscriber_id = :source_external_subscriber_id AND init_time >= :queryLastTstmp
GROUP BY a.`detail`
ORDER BY total desc");
$stmtOutgoing->execute(array('source_external_subscriber_id' => $source_external_subscriber_id, 'queryLastTstmp' => $queryLastTstmp));
Makes the statement work.
For some context:
A new table is created each month, hence the need to store the name in a variable unlike the first table which has a 'static' name.
The server is running 10.3.23-MariaDB
and PHP 7.2.33
.
I have tried quoting the variable like this: ':cdrLastTable'
but did not work.
Thanks for the help!