magento raw query with parameter binding
The variable $binds is an array of key value pairs. The key needs to be the same as the string in the raw query string that has a colon in front of it. In this case, :category_id is in the raw mysql query and it will be replaced by the value of the array element with the key of “category_id”. If the query has a limit in it, the binding doesn’t work for the limit value, thus, the limit value plugged in as it is.
[code language=”php”]
$cateId = 12345;
$limit = 10;
$read = Mage::getSingleton(‘core/resource’)->getConnection(‘core_read’);
$sqlQuery = "SELECT product_id FROM catalog_category_product WHERE category_id = :category_id ORDER BY RAND() LIMIT $limit";
$binds = array(‘category_id’ => $cateId);
$results = $read->query($sqlQuery, $binds);
while($result = $results->fetch()) {
echo $result[‘product_id’];
}
[/code]
Search within Codexpedia

Search the entire web
