replacing sql calls

  • So I have an old module, that is using oldscool sql calls. How would I replace…

    $GLOBALS['___mysqli_st'] = mysqli_connect(_DB_SERVER_, _DB_USER_, _DB_PASSWD_);
    $sql2 = mysqli_query($GLOBALS['___mysqli_st'], '
                SELECT *
                FROM `'._DB_PREFIX_.'product_shop`
                WHERE `id_product` =  '.(int)$idc['id_product'].' AND `id_shop` = '.(int)$this->context->shop->id.'
    $result2  = mysqli_fetch_assoc($sql2);
    $count2  = mysqli_num_rows($sql2);

  • $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(
                (new DbQuery())
                    ->from('product_shop', 'ps')
                    ->where('ps.`id_product` = '.(int) $idc['id_product'])
                    ->where('ps.`id_shop` = '.(int) $this->context->shop->id)

  • I thought…
    $result2 = mysqli_fetch_assoc($sql2);
    $count2 = mysqli_num_rows($sql2);

    would be…
    $result2 = Db::getInstance(PS_USE_SQL_SLAVE)->getRow($sql2);
    count2 = Db::getInstance()->numRows();

    but I’m confused on how to change sql2 to a newer style. tb1.0.2 type style…

  • getRow will return only single row (the first one).
    executeS will return array of all matching rows.
    To get the count, you can just count the result (count($result))

  • I didnt give enough of the code. The key was changing the do/while loop to a foreach loop. Thanks for the help tho…


Looks like your connection to thirty bees forum was lost, please wait while we try to reconnect.