dongzheng7165 2014-10-06 14:33
浏览 10

函数生成有效的sql语句,失败就像非对象一样

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');
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度