简体   繁体   中英

understanding concept to get mysql data

I can handle mysql data well using this approach:

        $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'];

I hope it's standard way.

Currently I came accross one new way to do same:

    $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'));

If table accounts has values name , id . How can get access of it?

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'];     
}

Here is a brief example of your code put into a small class wrapper. For simplicity I'm not checking for bad user input, though this should normally always be done.

This example is for testing purposes and could of course be made a lot nicer ;-).

I also haven't had the opportunity to test this code so please excuse me if there are any errors. This code is Expecting your database has two tables "tb_users" and "tb_accounts".

The Database wrapper:

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

The Model wrapper:

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

Executing the code:

$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);
}

Hope this helps you a little.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM