簡體   English   中英

了解概念以獲取mysql數據

[英]understanding concept to get mysql data

我可以使用這種方法很好地處理mysql數據:

        $con = mysqli_connect('127.0.0.1', 'root', 'root', 'root');
        if (mysqli_connect_errno())
        {
                echo "Failed to connect to MySQL: " . mysqli_connect_error();
                return;
        }
        $select1 = mysqli_query($con,"SELECT * from review_words");

        $record =  @mysqli_fetch_assoc($select1);
        echo $record['noun'];

我希望這是標准方式。

目前,我遇到了一種新的方法:

    $this->db = new Database($config->getDbHost(), $config->getDbUsername(), $config->getDbPassword(), $config->getDbName());
    $this->table = array
    (
        'users'=>$this->table_prefix.'user_t', 
        'accounts' => $this->table_prefix.'accounts_t'
    )
    function getT($name)
    {
          return $this->table[$name];
    }
    $this->db->select("SELECT * FROM ".$this->getT('accounts'));

如果表accounts具有值nameid 如何獲得它?

UPDATE

function validateRequest($key)
{    
    $this->db->select("SELECT user_id FROM ".$this->getT('accounts')."where account_key = '".$key."'");
    $data = $this->db->execute();
    $userid = $data['user_id'];

    $this->db->select("SELECT user_status FROM ".$this->getT('users')."where user_id = '".$userid."'");
    $data = $this->db->execute();
    $userstatus = $data['user_status'];     
}

這是將代碼放入小型類包裝器中的簡短示例。 為簡單起見,我不檢查用戶輸入是否錯誤,盡管通常應始終這樣做。

這個例子是出於測試目的,當然可以做得更好;-)。

我也沒有機會測試此代碼,所以如果有任何錯誤,請原諒。 這段代碼期望您的數據庫有兩個表“ tb_users”和“ tb_accounts”。

數據庫包裝器:

class Database {

    public $db; 
    private $sql;   

    public function __construct($host, $user, $password, $database) {

        $this->db = new mysqli($host, $user, $password, $database);

        /* check connection */
        if ($this->db->connect_errno) {
            throw new Exception("Failed to connect to MySQL: (" . $this->db->connect_errno . ") " . $this->db->connect_error, 1);
        }
    }

    public function select($table, $cols) {

            $colsearch = (is_array($cols)) ? implode(", ", $cols) : $cols;
            $sql = "SELECT " . $colsearch . " from " . $table;
    }

    public function execute() {
        if (! is_null($this->sql)) {
            $rs = $this->db->query($this->sql);

            if($rs === false) {
                throw new Exception('Problematic SQL: ' . $this->sql . ' Error: ' . $this->db->error, 1);
            } else {
                return $rs->fetch_all(MYSQLI_ASSOC);
            }

        } else {
            return null;
        }   
    }
}

模型包裝器:

class MyModel {

    private $tbl_prefix = "tb_";

    private $tables;

    public function __construct() {
        $this->tables = array(
            "users" => $this->tbl_prefix . "users",
            "accounts" => $this->tbl_prefix . "accounts"
        );
    }

    public function getTableByName($name) {
        if (array_key_exists($name, $this->tables)) {
            return $this->tables[$name];
        } else {
            return null;
        }
    }
}

執行代碼:

$myDatabase = new Database('127.0.0.1', 'root', 'root', 'root');

$myModel = new MyModel();

$myDatabase->select($myModel->getTableByName("users"), array(
    "name",
    "id"
));

$data = $myDatabase->execute();

foreach ($data as $item) {
    print_r($item);
}

希望這對您有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM