Berny
Berny

Reputation: 121

Getting PDO exception 42000 on PDO SQL query

This has been bugging me for a fair amount of time now and I can't seem to find were the issue lies. This is the query I'm stuck at:

$stmt2 = $conn->prepare('SELECT a.[zone], b.[source_external_subscriber_id], SUM(b.[source_customer_cost]) AS [total]
    FROM billing_zones a INNER JOIN cdr b 
    ON a.[id] = b.[source_customer_billing_zone_id] 
    WHERE destination_account_id = :destinationId 
    GROUP BY b.[source_external_subscriber_id], a.[zone]');
$stmt2->execute(array('destinationId' => $destinationId));

The message I get:

Uncaught PDOException: 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 '[zone], b.[source_external_subscriber_id], SUM(b.[source_customer_cost] AS [tota' at line 1 in /home/integracion.php:14

Upvotes: 0

Views: 97

Answers (1)

Cid
Cid

Reputation: 15247

Squared brackets ([...]) are for Microsoft SQL Server, not for MySQL/MariaDB. You need to use backticks instead

--       v----v------- check this
SELECT a.`zone` ...

Note that you can use double quotes too if you enable SQL_MODE=ANSI_QUOTE. Double quotes are SQL-92 standard and should be compatible with any DBMS

Upvotes: 3

Related Questions