dongxi2163 2017-11-01 06:58
浏览 51
已采纳

Prestashop 1.6.1.14 - 数据库查询在Execute上返回1

I am currently using the code below in Prestashop to retrieve a cart id.

    public function hookDisplayPDFInvoice($params) {

    $order_invoice = $params['object'];
    $id_order = (int)$order_invoice->id_order;

    $sql = 'SELECT id_cart FROM '._DB_PREFIX_.'orders WHERE id_order="'.$id_order.'"';
    //example id_cart
    $id_cart = Db::getInstance()->execute($sql);
    return $id_cart;

In database, there are id_cart and id_timeslot. Table is called ps_cart_timeslot.

I am quite stuck as I am baffled as to why the data return is 1 for any data I am retrieving.

$id_order is fine, it is returning the right value. Any data select I am querying will return 1.

Am I missing anything? Pardon me if this is a silly mistake.

Thank you.

  • 写回答

1条回答 默认 最新

  • dongmie3526 2017-11-01 09:12
    关注

    For selects use Db::getInstance()->executeS($sql); or to get single value use Db::getInstance()->getValue($sql);

    However since PS 1.6 you should be using query builder.

    $query = new DbQuery();
    $query->select('id_cart')
          ->from('orders')
          ->where('id_order = ' . (int)$id_order);
    Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
    // Or array of values
    Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
    

    execute() method returns only true or false while executeS() will return an array of select results (method can only be used for select queries) and getValue() will return the first value found in result.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线