首頁  >  問答  >  主體

誰有原始碼,貼一下

自己程式碼一直錯誤: Fatal error: Call to a member function exec() on null 

木俞木俞2383 天前1322

全部回覆(1)我來回復

  • 秋凉

    秋凉2018-03-14 21:10:44


    ##class db{

    private $dbConfig = [

    'db' => 'mysql',

    '主機' => 'localhost',

    '連接埠' => '3306',

    '資料庫名稱'=>  'one',

    '使用者' => 'root',

    '密碼' => '1234',//

    '字元集' => 'utf'

    ];


    ## public $insertId = null;


    # public $num = 0;

    私人靜態$instance = null;


    ## 私人$conn = null;



    私有函數 __construct($params){

    $this->dbConfig = array_merge($this->dbConfig,$params);

    # $this->conncet();

    }

    # 私人函數__clone(){


    # }

    公用函數getInstance(){

    if(!self::$instance instanceof self){

    self::$instance = new self();

    }

    return self::$instance;

    }

    public function connect(){

    try{

    ## $dsn = " { $this->dbConfig['db']}:{$this->dbConfig['host']};

    port = {$this->dbConfig['port']};

    字元集= {$this->dbConfig['charset']}";

    $this->conn = new pdo($dsn,$this->dbConfig['user '],$ this->dbConfig['password']);

    }

    catch(PDOExecption $e){

    die("資料庫連線失敗". $e->getMessage ());

    }

    }

    //完成資料表的寫入操作新增更新刪除

    # //傳回出行的記錄如果新增也回傳新增主鍵id

    public function exec($sql){

    $num = $this->conn->exec($sql );

    # / / 如果有旅行的記錄

    if($num>0){

    // 如果是新增操作初始化新增主鍵ID的屬性

    if(null ! ==$this->conn->lastInsertId()){

    $this->insertId = $this->conn->lastInsertId( );

    }

    $this->num = $rum;//返回旅行的記錄

    }else{

    $error = $this->conn->errorInfo(); //取得最後操作的錯誤訊息 【0】錯誤標識符【1】錯誤代碼【2】錯誤訊息

    print '操作失敗'.$error[0].':'.$error[1 ].':'.$error[2];

    }

    }

    ## //取得單一查詢資訊

    public function fetch ($sql )

    {

    return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC);

    }

    public function fetchALL($sql){

    return $this->conn->query($sql)->fetch(PDO::FETCH_ASSOC );

    }

    }

    #

    回覆
    0
  • 取消回覆