Jump to content

ERROR WHEN USING DBQUERY


Recommended Posts

Good morning, I am having problems using dbquery to query my database,

When I run the following code:

<?php

$sql = new DbQuery();

$sql->select('*');

$sql->from('ps_orders', 'o');

print($results = Db::getInstance()->executeS($sql));

?>

 

I get a 500 error from the server, I don't know if this is because the server is rejecting the SQL connection or I am doing something wrong in the code.

Link to comment
Share on other sites

  • 3 weeks later...

Hello @adrianfp2000,

Here are some corrections in the code. Please use this code to retrieve data from the orders table:

<?php
// Include PrestaShop configuration file
require_once(dirname(__FILE__) . '/config/config.inc.php');
require_once(dirname(__FILE__) . '/init.php');

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

use PrestaShop\PrestaShop\Adapter\Entity\DbQuery;
use PrestaShop\PrestaShop\Adapter\Entity\Db;

// Create a new DbQuery instance
$sql = new DbQuery();

try {
    // Build the query
    $sql->select('*');
    $sql->from('orders'); // Use the correct table prefix

    // Execute the query and fetch results
    $results = Db::getInstance()->executeS($sql);

    // Check if results are returned and print them
    if ($results) {
        echo '<pre>';
        print_r($results);
    } else {
        echo 'No results found.';
    }
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...