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?
array($id)? what you are passing here.$idin this instance is the primary key of the table, its surrounded byarray()because usually there are a few more values parsed in there forWHERE, ORDER BYetc. though in this instance it could be left out. Running it without thearray()returns identical results.