Moose
Moose

Reputation: 630

Magento SQL statement not returning expected results

I'm running Magento 1.7.0.2:

I'm trying to retrieve all the columns from a custom table using PHP & SQL, but the results returned are not what I expected or usually get:

$connection = $this->_getConnection('core_read');
$sql = "SELECT * FROM " .  $this->_getTableName('my_custom_table_name') . " cped
        WHERE  cped.id = ?";
$results = $connection->fetchOne($sql, array($id));
print_r($results); //this only prints out a single value

public function _getConnection($type = 'core_read'){
     return Mage::getSingleton('core/resource')->getConnection($type);
}

public function _getTableName($tableName){
     return Mage::getSingleton('core/resource')->getTableName($tableName);
}

The issue is, this only returns the first column (i.e in this case id) even though I've used the

Select *

Statement, which usually works perfectly fine. Coincidentally, if I try specify the column names that I'm interested in using:

Select id, name, sku, custom_value

It only returns the first value, so whichever column I specify first is the value it returns.

If I try running this same statement in PHPMyAdmin, it returns the expected results perfectly. Any ideas?

Upvotes: 0

Views: 866

Answers (2)

Moose
Moose

Reputation: 630

Turns out I was mistaken about the functionality of the direct SQL statements fetchOne will indeed fetch only one column result from the statement, the fetchRow query will return every column in that table.

Upvotes: 0

Marius
Marius

Reputation: 15216

That is what fetchOne does. It gets the first record. Notice the One in the function name.
Try using fetchAll.

Upvotes: 4

Related Questions