How to make MySQL IN statements using Zend DB?

I am trying to get strings that are in an array of integers that I have using Zend Framework 1.11.

$this->dbSelect ->from($table_prefix . 'product_link') ->joinLeft($table_prefix . 'product_link_name', $table_prefix . 'product_link.product_link_name_ref_id = ' . $table_prefix . 'product_link_name.product_link_name_id') ->where('product_ref_id IN (?)', implode(', ', $product_ids)); 

When I use the __toString() $this->dbSelect , I get

 SELECT `phc_distrib_product_link`.*, `phc_distrib_product_link_name`.* FROM `phc_distrib_product_link` LEFT JOIN `phc_distrib_product_link_name` ON phc_distrib_product_link.product_link_name_ref_id = phc_distrib_product_link_name.product_link_name_id WHERE (product_ref_id IN ('10, 12')) 

This returns rows satisfying the condition when product_ref_id = 10. How can I get the IN clause

 product_ref_id IN ('10', '12') 

or

 product_ref_id IN (10, 12) 

using the prepared Zend DB statements so that I can get all the rows contained within the array of product identifiers?

+8
mysql escaping prepared-statement zend-framework
source share
3 answers

Do not insert an array into an array, just pass it:

 ->where('product_ref_id IN (?)', $product_ids); 
+15
source share

It is worth mentioning that there are two ways to use the WHERE IN clausule in Zend_Db_Select:

  • We can pass the array as the second parameter:

    $select->where("column_value IN (?)", $array_of_values)

  • Or we can just blow up the array to get the values ​​as strings:

    $select->where("column_value IN (" . implode(',', $array_of_values) . ")")

+1
source share
 ->where('country_id IN (?)', $country_ids); 
-one
source share

All Articles