views:

26

answers:

2

Hi there,

I'm receiving the following error with this page while trying to retrieve information from my database;

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 'FROM catalog_product_entity cpe inner join catalog_product_entity_varchar cpev o' at line 5

The code I am using is as follows;

include("conn.php");

//Get all products that are configurable
$query  = "SELECT cpe.entity_id entity,
cpe.sku sku,
cpe.category_ids categories,
cpev.value title,
FROM catalog_product_entity cpe inner join catalog_product_entity_varchar cpev on cpe.entity_id = cpev.entity_id
WHERE cpe.type_id = 'configurable' LIMIT 0,30";
$result = mysql_query($query) or die(mysql_error());

while($row = mysql_fetch_assoc($result))
{
    echo "id :{$row['entity']} <br>" .
         "sku :{$row['sku']} <br>" .
         "value :{$row['title']} <br>" .
         "Categories : {$row['categories']} <br>";
} 

What I am trying to do is to retrieve products from a magento database to display on a non-magento site.

Wondered if anyone might be able to help me resolve this error.

Thanks

+2  A: 

You simply have a dangling comma at the end of the selected fields, before the FROM clause:

cpev.value title,

should be:

cpev.value title
Daniel Vassallo
Oh man. I'm ready to go home! Ha. Thank you. :D
Neil Bradley
A: 

Another approach would be to use the Magento classes to form the query for you.

require 'app/Mage.php';
Mage::app();

$products = Mage::getModel('catalog/product')
    ->getCollection()
    ->addAttributeToFilter('type_id', 'configurable')
    ->setPage(30, 0);

foreach ($products as $product) {
    echo nl2br("id: {$product->getId()}
        sku: {$product->getSku()}
        value: {$product->getTitle()}
        Categories: {$product->getCategoryIds()}");
}

The advantage is it automatically uses the correct database credentials even if they are changed. You also benefit from methods like $product->getCategoryCollection() should you want to retrieve the category names or any other detail.

This way does have the overhead of loading Magento which might make your page a little slower.

clockworkgeek