pg.ddx.io  pgsql-php@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Select Where using character varying ??
5+ messages / 5 participants
[nested] [flat]

* Select Where using character varying ??
@ 2006-10-03 04:08  ben wilko <wilko_ben@hotmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: ben wilko @ 2006-10-03 04:08 UTC (permalink / raw)
  To: pgsql-php

<html><div style='background-color:'><DIV class=RTE>Hi Guys</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>I am printing names into a combo box and posting the variable. I try and use the name ($Sem) in an SQL statement; WHERE&nbsp;name = $Sem; but I get an error which is displayed&nbsp;near the end of this message. Are we able compare php string to postgresql character varying??&nbsp; not sure how else to do the selection</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>$conn = pg_Connect("host=localhost dbname=#### user=#### password=####");<BR>&nbsp; &nbsp;if (!$conn) {echo "An database connection error occurred.\n"; exit;}</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>// e.g. $Sem = "seminar one";</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>$Sem = $_POST['Seminars'];</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>$Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE&nbsp;name = $Sem");<BR>&nbsp;if (!$SemNo) {echo "A query error occurred in retreiving the selected seminar's ID &lt;br&gt;"; /*exit;*/}<BR>$Sem_No = pg_Result($Sem_No, 0);</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>// Error Message</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE><STRONG>Warning</STRONG>: pg_exec() [<A href="http://www.citanalyst.com/jet/form/function.pg-exec">function.pg-exec</A;]: Query failed: ERROR: syntax error at or near "one" at character 54 in <B>/home/bdwilko/public_html/jet/form/insertP.php</B> on line <B>56</B><BR>A query error occurred in retreiving the selected seminar's ID </DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>&nbsp;</DIV>
<DIV class=RTE>Table:&nbsp;<TD> </TD><TD><A title=Table href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/redirect.php?server=%3A5432&amp;database=bd...;
<DIV class=RTE><TR><TH class=data><TR><TH class=data>Column</TH>&nbsp;&nbsp;&nbsp;&nbsp; <TH class=data>Type</TH> <TH class=data>Not Null</TH> <TH class=data>Default</TH> <TH class=data colspan="2">Actions</TH> <TH class=data>Comment</TH> </DIV>
<DIV class=RTE></TR><TR><TD class=data1>seminar_id</TD> <TD class=data1>integer</TD> NOT NULL nextval('seminar_seminar_id_seq'::regclass)</TD> <TD class=opbutton1><A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=properties&amp;ser... class=opbutton1><A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=confirm_drop&amp;s... class=data1></TD> </DIV>
<DIV class=RTE></TR><TR><TD class=data2>name</TD>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <TD class=data2>character varying</TD> <A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=properties&amp;ser... class=opbutton2><A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=confirm_drop&amp;s... class=data2></TD> </DIV>
<DIV class=RTE></TR><TR><TD class=data1>address</TD>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <TD class=data1>character varying</TD> <A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=properties&amp;ser... class=opbutton1><A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=confirm_drop&amp;s... class=data1></TD> </DIV>
<DIV class=RTE></TR><TR><TD class=data2>date</TD>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <TD class=data2>date</TD>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=properties&amp;ser... class=opbutton2><A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=confirm_drop&amp;s... class=data2></TD> </DIV>
<DIV class=RTE></TR><TR><TD class=data1>description</TD>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <TD class=data1>character varying <A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=properties&amp;ser... class=opbutton1><A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=confirm_drop&amp;s... class=data1></TD> </DIV>
<DIV class=RTE></TR><TR><TD class=data2>notes</TD>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <TD class=data2>character varying</TD> <A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=properties&amp;ser... class=opbutton2><A href="https://www13.a2hosting.com:2083/3rdparty/phpPgAdmin/tblproperties.php?action=confirm_drop&amp;s... class=data2></TD></TR><TD class=data1></DIV>
<DIV class=RTE align=center></DIV>
<DIV class=RTE></TD><TD class=data1></TD><TD class=opbutton1></TD><TD class=data1></TD></TR></DIV></div></html>




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

* Re: Select Where using character varying ??
@ 2006-10-03 08:05  Charley Tiggs <lists@tiggs.net>
  parent: ben wilko <wilko_ben@hotmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Charley Tiggs @ 2006-10-03 08:05 UTC (permalink / raw)
  To: ben wilko <wilko_ben@hotmail.com>; +Cc: pgsql-php

ben wilko wrote:
> Hi Guys
>  
> I am printing names into a combo box and posting the variable. I try and use the 
> name ($Sem) in an SQL statement; WHERE name = $Sem; but I get an error which is 
> displayed near the end of this message. Are we able compare php string to 
> postgresql character varying??  not sure how else to do the selection
>  
>  
> $conn = pg_Connect("host=localhost dbname=#### user=#### password=####");
>    if (!$conn) {echo "An database connection error occurred.\n"; exit;}
>  
>  
> // e.g. $Sem = "seminar one";
>  
> $Sem = $_POST['Seminars'];
>  
>  
> $Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name = $Sem");
>  if (!$SemNo) {echo "A query error occurred in retreiving the selected seminar's 
> ID <br>"; /*exit;*/}
> $Sem_No = pg_Result($Sem_No, 0);
>  
> // Error Message
>  
> *Warning*: pg_exec() [function.pg-exec 
> <http://www.citanalyst.com/jet/form/function.pg-exec;]: Query failed: ERROR: 
> syntax error at or near "one" at character 54 in 
> */home/bdwilko/public_html/jet/form/insertP.php* on line *56*
> A query error occurred in retreiving the selected seminar's ID
>  

Make sure that you're quoting the value you want to compare.  At a quick 
glance, this where the error is:

$Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name = $Sem");

should be:

$Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name = 
'$Sem'");

To be more accurate, you should probabley do:

$Sem = pg_escape ( $Sem );
$Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name = 
'$Sem'");

Charley



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

* Re: Select Where using character varying ??
@ 2006-10-03 12:57  DCarrero <dcarreroc@gmail.com>
  parent: Charley Tiggs <lists@tiggs.net>
  0 siblings, 1 reply; 5+ messages in thread

From: DCarrero @ 2006-10-03 12:57 UTC (permalink / raw)
  To: Charley Tiggs <lists@tiggs.net>; +Cc: ben wilko <wilko_ben@hotmail.com>; pgsql-php

2006/10/3, Charley Tiggs <lists@tiggs.net>:
> ben wilko wrote:
> > Hi Guys
> >
> > I am printing names into a combo box and posting the variable. I try and use the
> > name ($Sem) in an SQL statement; WHERE name = $Sem; but I get an error which is
> > displayed near the end of this message. Are we able compare php string to
> > postgresql character varying??  not sure how else to do the selection
> >
> >
> > $conn = pg_Connect("host=localhost dbname=#### user=#### password=####");
> >    if (!$conn) {echo "An database connection error occurred.\n"; exit;}
> >
> >
> > // e.g. $Sem = "seminar one";
> >
> > $Sem = $_POST['Seminars'];
> >
> >
> > $Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name = $Sem");
> >  if (!$SemNo) {echo "A query error occurred in retreiving the selected seminar's
> > ID <br>"; /*exit;*/}
> > $Sem_No = pg_Result($Sem_No, 0);
> >
I think you should try:
$Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name =\"$Sem\"");
OR
$Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name ='$Sem'");
OR
$Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name
='".$Sem."';";);
or something like this for escape "seminar one" with " " inside the sql query

But i look the table description you still compare a integer row with
a string row
try maki the query like this:
$Sem_No = pg_Exec($conn,"SELECT name FROM seminar WHERE name =
 '$Sem'");
> > // Error Message
> >
> > *Warning*: pg_exec() [function.pg-exec
> > <http://www.citanalyst.com/jet/form/function.pg-exec;]: Query failed: ERROR:
> > syntax error at or near "one" at character 54 in
> > */home/bdwilko/public_html/jet/form/insertP.php* on line *56*
> > A query error occurred in retreiving the selected seminar's ID
> >
>
> Make sure that you're quoting the value you want to compare.  At a quick
> glance, this where the error is:
>
> $Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name = $Sem");
>
> should be:
>
> $Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name =
> '$Sem'");
>
> To be more accurate, you should probabley do:
>
> $Sem = pg_escape ( $Sem );
> $Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name =
> '$Sem'");
>
> Charley
>
> ---------------------------(end of broadcast)---------------------------
> TIP 9: In versions below 8.0, the planner will ignore your desire to
>        choose an index scan if your joining column's datatypes do not
>        match
>
Daniel Carrero Canales



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

* Re: Select Where using character varying ??
@ 2006-10-03 20:03  Mariusz Pękala <skoot@qi.pl>
  parent: DCarrero <dcarreroc@gmail.com>
  0 siblings, 1 reply; 5+ messages in thread

From: Mariusz Pękala @ 2006-10-03 20:03 UTC (permalink / raw)
  To: pgsql-php

> I think you should try:
> $Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name 
> =\"$Sem\"");

Double quotes are for quoting column names, not string constants.

> $Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name 
> ='$Sem'");

Better, but all strings, especially provided by some user, should be
treated by the function pg_escape_string.

Consider that some user types in a form field a text like this:

'; delete from seminar where ''='

When you add single quotes you get two valid queries. One of them is
what you would never want to be executed ;-)

And, by the way - pg_exec is a deprecated name AFAIK. The new one is
pg_query.


-- 
Ceterum censeo Internet Explorer esse delendam.

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

* Re: Select Where using character varying ??
@ 2006-10-04 00:48  Robert Treat <xzilla@users.sourceforge.net>
  parent: Mariusz Pękala <skoot@qi.pl>
  0 siblings, 0 replies; 5+ messages in thread

From: Robert Treat @ 2006-10-04 00:48 UTC (permalink / raw)
  To: pgsql-php; +Cc: Mariusz Pękala <skoot@qi.pl>

On Tuesday 03 October 2006 16:03, Mariusz Pękala wrote:
> > I think you should try:
> > $Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name
> > =\"$Sem\"");
>
> Double quotes are for quoting column names, not string constants.
>
> > $Sem_No = pg_Exec($conn,"SELECT seminar_id FROM seminar WHERE name
> > ='$Sem'");
>
> Better, but all strings, especially provided by some user, should be
> treated by the function pg_escape_string.
>
> Consider that some user types in a form field a text like this:
>
> '; delete from seminar where ''='
>
> When you add single quotes you get two valid queries. One of them is
> what you would never want to be executed ;-)
>
> And, by the way - pg_exec is a deprecated name AFAIK. The new one is
> pg_query.

probably even better would be to use pg_prepare and pg_execute. 

-- 
Robert Treat
Build A Brighter LAMP :: Linux Apache {middleware} PostgreSQL



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


end of thread, other threads:[~2006-10-04 00:48 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2006-10-03 04:08 Select Where using character varying ?? ben wilko <wilko_ben@hotmail.com>
2006-10-03 08:05 ` Charley Tiggs <lists@tiggs.net>
2006-10-03 12:57   ` DCarrero <dcarreroc@gmail.com>
2006-10-03 20:03     ` Mariusz Pękala <skoot@qi.pl>
2006-10-04 00:48       ` Robert Treat <xzilla@users.sourceforge.net>

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