0

I am using PHP with PostgreSQL (PDO) and I am wondering is there a way to not have to wrap my SQL statement columns in double quotation marks when making a call? For example, I have the following code that retrieves the column name needed

$columnname = $_GET["columname"];

but since PostgreSQL requires a column to be inside a double quotation mark, I then have to modify the code to the following

$columnname = '"' . $_GET["columname"] . '"';
Robin
  • 83
  • 1
  • 7
  • `but since PostgreSQL requires a column to be inside a double quotation mark` ... why do you think this is the case? Can you add more explanation to your question? – Tim Biegeleisen Mar 16 '21 at 04:40
  • @TimBiegeleisen When I go to my pgadmin, it requires the column to be wrapped with double quotes: `SELECT "OrderDate", "ShippedDate", "ShipName", "ShipAddress", "ShipCity", "ShipCountry" FROM orders WHERE ("ShipCity" LIKE '%Bern%')` If I remove the double quotes, it doesn't work. – Robin Mar 16 '21 at 04:49
  • 1
    Stop creating your column names in camel case, especially on Postgres, which is case sensitive. Now, you will have to use double quotes around those column names everywhere, forever. Instead, always use lowercase column and table names to avoid this problem. – Tim Biegeleisen Mar 16 '21 at 04:56
  • https://stackoverflow.com/questions/20878932/are-postgresql-column-names-case-sensitive – richyen Mar 16 '21 at 04:57
  • @TimBiegeleisen I did not know that, that is a weird feature thanks. How do I change it to lowercase? – Robin Mar 16 '21 at 05:16
  • 1
    You'll need to just alter each column with `ALTER TABLE orders RENAME COLUMN "OrderDate" TO orderdate;` (or drop and re-create the table with no double-quotes, and Postgres will lowercase them all for you) – richyen Mar 16 '21 at 06:18

0 Answers0