phppdoodbcwhitespace

Escaping white space in the column name in PDO odbc


I have an access database I need to connect to, which has a table named [Received Projects] and in that table, we have fields named [Assigned To], [Assigned Date], and [Image Cnt]. MS Access and odbc in general would be okay with a select statement like:

SELECT [Received Projects].[Assigned To], [Received Projects].[Assigned Date], [Received Projects].[Image Cnt]
FROM [Received Projects]
WHERE ([Received Projects].[Image Cnt])>0)

However, I'm trying to connect with PDO in PHP, which does not like the square brackets. It sort of likes something like this:

$strSQL = 'SELECT "Assigned To", "Assigned Date", "Image Cnt"
FROM "Received Projects"
WHERE ("Received Projects"."Image Cnt")>0)';

Except, it throws this error, indicating it doesn't know what the table is:

PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42P01]: Undefined table: 7 ERROR: relation "Received Projects" does not exist

I need to escape the white space in the table name, somehow. Double quotes works on the column name, but not the table name. I have tried single quotes, double quotes, backticks, and squarebrackets with variations of quotes. Starting to wonder if it's not possible.


Solution

  • try the below query. Its working fine ( TESTED )

    $strSQL = 'SELECT `Assigned To`, `Assigned Date`, `Image Cnt` 
    FROM `received projects` 
    WHERE `received projects`.`Image Cnt` > 0';
    

    Hope this fulfill your requirement. :) NOTE: Please avoid white space. Better to use Camel naming convention.