Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a string in query without using quotes ''

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%");
like image 242
Ettore Avatar asked Jan 15 '23 02:01

Ettore


2 Answers

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.

like image 132
Álvaro González Avatar answered Jan 17 '23 19:01

Álvaro González


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

$conditions[]=array('codiceBiblio IN (' . $tot . ')');
like image 39
RichardAtHome Avatar answered Jan 17 '23 17:01

RichardAtHome