tags:

views:

61

answers:

4

I'm using php 5.2 with oracle 11.1.

This code:

$query = oci_parse($conn, "SELECT * FROM COMMENTS WHERE PINID=$pinID and COMMENTID=$commentID");

results in this error:

<b>Warning</b>:  oci_execute() [<a href='function.oci-execute'>function.oci-execute</a>]: ORA-00904: &quot;COMMENTS&quot;: invalid identifier in <b>C:\IODwww\hello.php</b> on line <b>159</b><br />

^

But running this works fine:

$query = oci_parse($conn, "SELECT * FROM COMMENTS WHERE PINID=$pinID and COMMENTID=1");

If this a result of me injecting multiple variables into the query string, or am I making some other mistake?

A: 

Have you tried putting the variables within brackets?

$query = oci_parse($conn, "SELECT * FROM COMMENTS WHERE PINID={$pinID} and COMMENTID={$commentID}");

Also make sure that $commentID is not returning a blank value which would leave just COMMENTID= at the end and would cause an error when trying to run the query.

animuson
the brackets aren't changing my results. If I build the query and then echo it I get this: SELECT * FROM COMMENTS WHERE PINID=6 and COMMENTID=1
AYoung
+2  A: 

oci_execute()'s warning is not PHP warning. There is something wrong with resulting query.

print it out and take a look at it.

Col. Shrapnel
the resulting query (before oci_parse) is this "SELECT * FROM COMMENTS WHERE PINID=6 and COMMENTID=1". Afterwards the query is" Resource id #3"
AYoung
`ORA-00904: "COMMENTS": invalid identifier`: The database does not know what COMMENTS is. +1
Billy ONeal
A: 

There is no problem with multiple variables in a php string.

The debug the Problem you can try

var_dump("SELECT * FROM COMMENTS WHERE PINID=$pinID and COMMENTID=$commentID");

and see if the output really matches

string(...) "SELECT * FROM COMMENTS WHERE PINID=1 and COMMENTID=1" 

the only things i can think of is that commentID is empty or contains a "\n" or something attached to it that causes the error.

The errorcode the database puts out "The column name entered is either missing or invalid." doesn't make much sense to me if works with =1

edorian
I get string(52) "SELECT * FROM COMMENTS WHERE PINID=6 and COMMENTID=1"
AYoung
+3  A: 

For both performance and SQL Injection reasons, you should be using placeholder variables, like so:

$query = oci_parse($conn, "SELECT * FROM COMMENTS WHERE PINID = :pinID and COMMENTID = :commentID");
oci_bind_by_name($query, ':pinID', $pinID, -1, SQLT_INT);
oci_bind_by_name($query, ':commentID', $commentID, -1, SQLT_INT);
oci_execute($query);
R. Bemrose
thanks do you have a link that explains that?
AYoung
Sure: http://php.net/oci_bind_by_name
R. Bemrose
Argh, sorry, PHP's short link for that function goes to the old version. The real link is http://www.php.net/manual/en/function.oci-bind-by-name.php
R. Bemrose