I have a PHP function written that takes a series of parameters and generates a string. That string is then used in a Magento $readConnection->fetchOne($thestring); request and fails to actually read from the database, indicating via error message that the call to fetchOne is on a non-object.
However, the Varien_DB method takes the string query as a parameter, and should return an array as the result.
function updateDB($tablename,$entity,$selector,$id){
$query = 'SELECT entity_id, `value` FROM '.$tablename.' WHERE entity_id = '.$entity.' AND '.$selector.' = '.$id;
$result = $readConnection->fetchOne($query);
}
The reason behind writing this as a function is that I have a large selection of products that get updated pricing on a daily basis, but not all receive the same updates. In addition, the prices are located on different tables in the database, so by passing the relevant values to the function I can run the appropriate query.
The $query generated by passing the values to the function is a valid string, and can be used by itself as a query against the database (verified through Navicat against the db). When the $query is used by $readConnection->fetchOne, it returns an error of "Call to a member function fetchOne() on a non-object", so I am not sure what is going on. The $query does not need to be an object, nor does it fail when run by itself against the db.
Per comments, $readConnection is defined prior to calling the function. I can set it as a global variable and see if that works.
$resource = Mage::getSingleton('core/resource');
$readConnection = $resource->getConnection('core_read');
$writeConnection = $resource->getConnection('core_write');