简体   繁体   English

查询在php中返回0个结果,但是在oracle SQL开发人员中它返回52个结果

[英]Query returns 0 results in php, but in oracle SQL developer it returns 52 results

I have a problem with with my oracle query. 我的oracle查询有问题。 When I run it in PHP, it returns 0 results. 当我在PHP中运行它时,它返回0个结果。 If I run the same Query in Oracle SQLDeveloper, it returns 52 results. 如果我在Oracle SQLDeveloper中运行相同的查询,它将返回52个结果。

This is the query in PHP: 这是PHP中的查询:

SELECT name , zipcity FROM  import_persons WHERE upper(:zoekop) LIKE '%:zoekwaarde%' AND status < 3 ORDER BY name
$parameters = array(':zoekop' => $zoekop, ':zoekwaarde' => $zoekwaarde);

The query I run in SQLDeveloper, with the variables filled in manually.(Copied them from an echo in the PHP). 我在SQLDeveloper中运行的查询带有手动填充的变量(从PHP中的回显中复制了它们)。

SELECT  name , zipcity FROM import_persons WHERE upper(name) LIKE '%Q%' AND status < 3 ORDER BY name

I can't seem to find the error, before asking, I tried to bind the params but that didn't work. 我似乎找不到错误,在问之前,我试图绑定参数,但这没有用。

There are two problems here: 这里有两个问题:

SELECT name , zipcity FROM  import_persons
    WHERE UPPER(:zoekop) LIKE '%:zoekwaarde%' AND status < 3
    ORDER BY name

It is good you're using bound parameters, but unfortunately only values can be bound; 最好使用绑定参数,但是不幸的是只能绑定值。 since :zoekop is a placeholder for a column, you'll have to use traditional string concatenation (and if the column name comes from user input, use a whitelist for security). 因为:zoekop是一列的占位符,所以您必须使用传统的字符串连接(如果列名来自用户输入,请使用白名单来确保安全性)。

You can bind for :zoekwaarde , but the wildcards need to be part of the string you bind, not in the query. 您可以绑定:zoekwaarde ,但是通配符必须是您绑定的字符串的一部分,而不是查询中。 Your SQL will therefore look a bit like this: 因此,您的SQL将看起来像这样:

SELECT name , zipcity FROM  import_persons
    WHERE UPPER(zoekop_col) LIKE :zoekwaarde AND status < 3
    ORDER BY name

Notice that the query does not include quotes - they are not required, since the binding system knows the type of the parameter you are binding in this position. 请注意,查询不包含引号-不需要引号,因为绑定系统知道您要在此位置绑定的参数的类型。 Wrapping it all up, you now only have one parameter to bind: 总结一下,您现在只需要绑定一个参数:

$parameters = array(':zoekwaarde' => "%{$zoekwaarde}%", );

i don't know PHP so this is a bit of a guess, but i wonder if the 我不知道PHP,所以这有点猜测,但是我想知道

LIKE '%:zoekwaarde%' 

is taking the variable name as a literal string and you many be better off with something like 将变量名作为文字字符串使用,例如

LIKE '%' || :zoekwaarde || '%'

(Posted on behalf of the OP.) (代表OP发布。)

Thanks to halfer's answer, I was able to fix the problem. 多亏了Halfer的回答,我才得以解决问题。 The fix: 解决方法:

$query_01 = "SELECT name , zipcity FROM  import_persons WHERE upper(name) LIKE :zoekwaarde AND status < 3 ORDER BY name";
$parameters = array(':zoekwaarde' => "%{$zoekwaarde}%", );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM