来自sales_flat_order表的信息

时间:2016-12-19 12:59:39

标签: php magento

我需要从 sales_flat_order 表中获取一些信息,以便在html文件中显示订单的付款信息。我怎么能够?我尝试使用一些PHP代码,但它没有返回任何内容。

1 个答案:

答案 0 :(得分:1)

所以这是一个基础示例,可以帮助您开始使用PHP。如果您需要从数据库获取查询,这是最合适的选项。

首先,将文件扩展名更改为.php而不是.html

然后:

创建数据库连接文件:

/**
 * 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;
    }
}

然后我建议制作一个dbCommon.php文件:

/**
 * 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;
    }
}

您可以从引导程序添加内容,例如:

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

在dbCommon.php文件中。

在您完成这些之后,您需要自己制作一个类文件来添加逻辑。以下是代码外观的基本示例:

/**
 * 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;
    }
}

最后,在此之后你需要回到file.php(最初是.html)并将其添加到顶部:

<?php

require_once ('class.queries.php');

$fetch = new queries();

$info = $fetch->sales();

?>

现在这意味着您可以根据自己的选择获取信息,并且只需回显$info->columnName

我不是想为你擦鼻子,但希望这会为你提供进入PDO并正确执行PHP查询的指导。

相关问题