0

我在 oracle 的 php 中有一个简单的查询:

    $query='select u.username,u.lastname,u.firstname,c.event,c.reason 
    from users u, events c 
    where c.created_by=u.user_id and 
    u.username!="foo" and 
    c.event > "2012-01-01"';

这通过 oci_parse 就好了....

但是为了让 oci_execute 不会因条件的“无效标识符”而窒息,需要进行翻译。如果我从语句中删除“and u.username!="foo" and c.event > '2012-01-01'”,上面的查询就可以正常工作,如下所示:

    $query='select u.username,u.lastname,u.firstname,c.event,c.reason 
    from users u, events c 
    where c.created_by=u.user_id';

构造语句以将用户和日期条件传递给 oci_execute 的正确方法是什么?

4

1 回答 1

0

在 Oracle 中,字符串由单引号分隔,'而标识符由双引号分隔,"因此您的查询应该是:

$query='select u.username,u.lastname,u.firstname,c.event,c.reason 
from users u, events c 
where c.created_by=u.user_id and 
u.username!=\'foo\' and 
c.event > \'2012-01-01\'';

您将使用双引号来标识具有混合大小写名称的对象,例如:

CREATE TABLE "testTable" (id number);

SELECT * FROM testTable ; /* fails with ORA-00942*/

SELECT * FROM "testTable"; /* succeeds */
于 2012-03-22T13:21:12.270 回答