Skip to content

Instantly share code, notes, and snippets.

@jonpontet
Created December 16, 2021 11:41
Show Gist options
  • Save jonpontet/efb70b711e73a97dd94fd850df8a3df2 to your computer and use it in GitHub Desktop.
Save jonpontet/efb70b711e73a97dd94fd850df8a3df2 to your computer and use it in GitHub Desktop.
How to execute SQL statements in PrestaShop
<?php
// SQL SELECT
// Simple SELECT
$sql = 'SELECT `id_authorization_role` FROM `' . _DB_PREFIX_ . 'authorization_role` t WHERE ' . implode(' OR ', $whereClauses);
// Or SELECT with INNER JOIN
$sql = 'SELECT * FROM ' . _DB_PREFIX_ . 'orders o
INNER JOIN ' . _DB_PREFIX_ . 'order_detail od
ON o.id_order = od.id_order
WHERE od.product_id = '. $var
);
$result = Db::getInstance()->executeS($sql);
foreach ($result as $row) {
// do something
}
// SQL INSERT
$sql = 'INSERT IGNORE INTO `' . _DB_PREFIX_ . 'module_access` (`id_profile`, `id_authorization_role`) VALUES (' . (int) $idProfile . ',' . (int) $idRole . ')';
if (!Db::getInstance()->execute($sql)) {
// failed
}
// SQL DELETE
$sql = 'DELETE FROM `' . _DB_PREFIX_ . 'access` WHERE `id_profile` = "' . $val . '"';
if (!Db::getInstance()->execute($sql)) {
// failed
}
// SQL UPDATE
$sql = 'UPDATE `' . _DB_PREFIX_ . 'attribute` SET `id_attribute_group` = ' . (int) $this->id . ' WHERE `id_attribute` = ' . (int) $value['id'];
if (!Db::getInstance()->execute($sql)) {
// failed
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment