pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Newbie Question-Quoting SQL
3+ messages / 3 participants
[nested] [flat]

* Newbie Question-Quoting SQL
@ 2009-04-27 19:56  Thomas Hart <tehart@me.com>
  0 siblings, 2 replies; 3+ messages in thread

From: Thomas Hart @ 2009-04-27 19:56 UTC (permalink / raw)
  To: pgsql-php

I have some old forms that I used for MySQL a few years ago, and I'm  
trying to use the with postgres, but I keep on getting a message that  
the query failed.

The connection string seems to be okay:

$link = pg_connect("host=localhost port=5432 dbname=toms_stuff  
user=postgres password=********");

The query string, however, seems to be messed up.

  $result = pg_query ($link, "Select * from Author");
  if (!$result) {
  	echo "Query failed.\n";
  	exit;
  }

I assume the fault is in how the Select statement is quoted, but I  
can't see what I'm doing wrong.

Any suggestions will be appreciated. Also can anyone give me a general  
idea of the rules for quoting postgreSQL in PHP?

Thanks for any help you can give.

Tom Hart



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Newbie Question-Quoting SQL
@ 2009-04-27 20:02  Leif B. Kristensen <leif@solumslekt.org>
  parent: Thomas Hart <tehart@me.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Leif B. Kristensen @ 2009-04-27 20:02 UTC (permalink / raw)
  To: pgsql-php

On Monday 27. April 2009, Thomas Hart wrote:

>  $result = pg_query ($link, "Select * from Author");

If the table really is named "Author" with a capital A, you'll need to 
quote it, because postgres will fold it to lowercase otherwise.
-- 
Leif Biberg Kristensen | Registered Linux User #338009
Me And My Database: http://solumslekt.org/blog/



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: Newbie Question-Quoting SQL
@ 2009-04-27 20:19  Andrew McMillan <andrew@morphoss.com>
  parent: Thomas Hart <tehart@me.com>
  1 sibling, 0 replies; 3+ messages in thread

From: Andrew McMillan @ 2009-04-27 20:19 UTC (permalink / raw)
  To: Thomas Hart <tehart@me.com>; +Cc: pgsql-php

On Mon, 2009-04-27 at 15:56 -0400, Thomas Hart wrote:
> I have some old forms that I used for MySQL a few years ago, and I'm  
> trying to use the with postgres, but I keep on getting a message that  
> the query failed.
> 
> The connection string seems to be okay:
> 
> $link = pg_connect("host=localhost port=5432 dbname=toms_stuff  
> user=postgres password=********");
> 
> The query string, however, seems to be messed up.
> 
>   $result = pg_query ($link, "Select * from Author");
>   if (!$result) {
>   	echo "Query failed.\n";
>   	exit;
>   }
> 
> I assume the fault is in how the Select statement is quoted, but I  
> can't see what I'm doing wrong.
> 
> Any suggestions will be appreciated. Also can anyone give me a general  
> idea of the rules for quoting postgreSQL in PHP?

They don't apply so much to PHP, as to PostgreSQL which follows the SQL
standard for quoting.  Identifiers should be quoted with " and character
strings should be quoted with '.

And then within PHP if you want to have " in a string it should be
escaped like:

"SELECT * FROM \"Author\""
or
'SELECT * FROM "Author"'

or, in the case of using a keyword as a column name:

'select "DATE" from mytable'

You can also escape characters in the PostgreSQL string, using \, but of
course the \ itself may need to be escaped in PHP...

In this string the ' is backslashed to PostgreSQL:

"INSERT INTO \"Author\" (name) VALUES( E'O\\'Connor' )"

Note that the PostgreSQL string needs to be preceded by an "E" if it has
escapes in it (in recent versions of PostgreSQL, at least).


All of this should be in the PostgreSQL manual:

http://www.postgresql.org/docs/8.4/static/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS


Cheers,
					Andrew McMillan.

------------------------------------------------------------------------
andrew (AT) morphoss (DOT) com                            +64(272)DEBIAN
     Building more free and open source software for New Zealanders
------------------------------------------------------------------------





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2009-04-27 20:19 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2009-04-27 19:56 Newbie Question-Quoting SQL Thomas Hart <tehart@me.com>
2009-04-27 20:02 ` Leif B. Kristensen <leif@solumslekt.org>
2009-04-27 20:19 ` Andrew McMillan <andrew@morphoss.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox