Categories


Archives


Recent Posts


Categories


Query Bind Syntax

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

This one is for me, since I seem to forget Magento’s query binding syntax every time I stay away from it fore more than a week.

$sql = "select foo
from bar
where baz = ?";

$query = Mage::getSingleton('core/resource')->getConnection('core_read')
->query($sql,array($value));

while($row = $query->fetch())
{
    var_dump($row);
}

The biggest gotcha is not put your bind params (?) in quotation marks

where baz = '?' ";

The confusing the parsing engine, and makes it think there aren’t the right number of tokens. There’s also a named paramater syntax, which my old man tendencies avoids.

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 21st September 2011

email hidden; JavaScript is required