Pass a string in query without using quotes ''

丶灬走出姿态 提交于 2019-12-10 16:52:15

问题


I have this code for the query:

$conditions[]=array('codiceBiblio IN (?)'=> $tot);

Where $tot is a string (eg: 2345,5657,4565,5678).
In this case, the query will be:

SELECT [...] WHERE codiceBiblio IN ('2345,5657,4565,5678')

But it will returns just the first record.

So it sould be:

SELECT [...] WHERE codiceBiblio IN (2345,5657,4565,5678)

How can I do it?


How the query is built

I have this code for a query:

// General Query
$conditions = array(
    'editore LIKE' => "%$e%",
    'titolo LIKE' => "%$t%"
);

And I fill up $conditions with user's choices, for example:

if ($anno&&$anno2)
    $conditions[] = array('anno BETWEEN ? AND ?' => array($anno,$anno2));

if (isset($menu)&&$menu!='')
    $conditions[]=array('classe LIKE' => "%$menu%");

回答1:


Just use an array and omit the IN() clause. The manual (Complex Find Conditions) provides this example:

array('Company.status' => array('inactive', 'suspended'))

... which produces the following SQL:

`Company`.`status` IN ('inactive', 'suspended')

If $tot is a string like 2345,5657,4565,5678 you'll need to explode() it first.

Disclaimer: This works in Cake 2, not sure about 1.2.




回答2:


Álvaro G. Vicario's suggestion is the correct one, but you can also do this:

$conditions[]=array('codiceBiblio IN (' . $tot . ')');


来源:https://stackoverflow.com/questions/15112779/pass-a-string-in-query-without-using-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!