<?php
class db{
    public $conn;
    public $result;
    public $num=0;
    public $lastInsertId;
    function __construct(){
        try{
            $this->conn = new PDO('mysql:host=localhost;dbname=guitian_db;charset=utf8', 'guitian_user', 'med2@she');
            $this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
            $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

        } catch (PDOException $e) {
            die ("Error!: " . $e->getMessage() . "<br/>");
        }
    }
    function query($sql){
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            echo "\nPDO::errorInfo():\n";
            print_r($this->conn->errorInfo());
        }
        $qresult = $this->conn->query($sql);
        foreach($qresult as $row){
            $this->result[] = $row;
            $this->num++;
        }
        return $this->result;
    }
    function insert($sql){
        $stmt = $this->conn->prepare($sql);
        if (!$stmt) {
            //echo "\nPDO::errorInfo():\n";
            //print_r($this->conn->errorInfo());
        }
        try{
            $stmt = $this->conn->prepare($sql)->execute();
            if (!$stmt) {
                $result=false;
            }else{
                $result=true;
                $this->lastInsertId = $this->conn->lastInsertId();
            }
            return $result;
        } catch (PDOException $e) {
            $result=$e->getMessage();
            return $result;
        }
        
    }
    function exec($sql){
        $this->conn->query($sql);
    }
}
?>