[Solved] Information from the sales_flat_order table [closed]


So here is a base example to get you started with PHP. If you need to get queries from a database this is the most suitable option.

Firstly, change your file extension to .php not .html

Then:

Create your database connect file:

/**
 * database.php
 */
class Database
{
    private $host = "localhost";
    private $db_name = "dbname";
    private $username = "username";
    private $password = "password";
    public $conn;

    public function dbConnection()
    {

        $this->conn = null;
        try
        {
            $this->conn = new PDO("mysql:host=" . $this->host . ";dbname=" . $this->db_name, $this->username, $this->password);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        }
        catch(PDOException $exception)
        {
            echo "Connection error: " . $exception->getMessage();
        }

        return $this->conn;
    }
}

I then suggest making a dbCommon.php file:

/**
 * dbCommon.php
 */

require_once ('database.php');

class DBCommon
{
    private $conn;

    /** @var Common */
    public $common;

    public function __construct()
    {
        $database = new Database();
        $db = $database->dbConnection();
        $this->conn = $db;
    }

    public function runQuery($sql)
    {
        $stmt = $this->conn->prepare($sql);
        return $stmt;
    }
}

You can add things from bootstrap such as:

public function error($message)
    {
        $this->messages[] = '<div class="alert alert-danger">' . $message . '</div>';
    }

within the dbCommon.php file.

After you’ve made these you will then need to make yourself a class file to add your logic to. Here’s a basic example of how your code should look:

/**
 * class.queries.php
 */

require_once ('dbCommon.php');

class queries extends DBCommon
{

    public function __construct()
    {
        parent:: __construct();
    }

    public function sales()
    {
        $stmt = $this->runQuery("SELECT * FROM `sales_flat_order`");
        $stmt->execute();

        $res = $stmt->fetch(PDO::FETCH_OBJ);

        return $res;
    }
}

FINALLY, after this you then need to go back into the file.php (originally .html) and add this into the top:

<?php

require_once ('class.queries.php');

$fetch = new queries();

$info = $fetch->sales();

?>

This now means you can fetch information as and how you choose to and you’d simply echo out $info->columnName

I’m not trying to wipe your nose for you but hopefully this will give you guidance to get into PDO and performing PHP queries correctly.

1

solved Information from the sales_flat_order table [closed]