Magento 1.9 SQL Syntax Error – SQLSTATE[42000]: Syntax Error or Access Violation: 1064

magento-1.9sql

I have got Magento plugin from dev, but while using its giving exception, please help me.

 SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3, query was: SELECT COUNT(*) FROM `askfordetailspro` AS `main_table`
 LEFT JOIN `catalog_product_entity` AS `prod` ON prod.entity_id = main_table.product_id
 LEFT JOIN `catalog_product_entity_varchar` AS `cpev` ON cpev.entity_id=main_table.product_id AND cpev.attribute_id=

Code:

    public function __construct() {
     parent::__construct();

    $this->setId('askfordetailsproGrid');
    $this->setDefaultSort('sent_date');
    $this->setDefaultDir('DESC');
    $this->setSaveParametersInSession(true);
}

protected function _prepareCollection() {
    $collection = Mage::getModel('askfordetailspro/askfordetailspro')
            ->getCollection();

    $prodNameAttrId = Mage::getModel('eav/entity_attribute')
            ->loadByCode('4', 'name')
            ->getAttributeId();
    $collection->getSelect()
            ->joinLeft(
                    array('prod' => 'catalog_product_entity'), 'prod.entity_id = main_table.product_id', array('sku')
            )->joinLeft(
            array('cpev' => 'catalog_product_entity_varchar'), 'cpev.entity_id=main_table.product_id AND cpev.attribute_id=' . $prodNameAttrId . '', array('name' => 'value')
    );

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

Best Answer

you have syntax error ' . $prodNameAttrId . '' try this query..

   $collection->getSelect()
                ->joinLeft(
                        array('prod' => 'catalog_product_entity'), 'prod.entity_id = main_table.product_id', array('sku')
                )->joinLeft(
                array('cpev' => 'catalog_product_entity_varchar'), 'cpev.entity_id=main_table.product_id AND cpev.attribute_id="' . $prodNameAttrId . '"', array('name' => 'value')
        );
Related Topic