Mysql使用类绑定插入:无法使用oops插入

时间:2016-11-02 12:01:29

标签: php mysqli

我正在学习课程,我正在尝试使用mysql绑定参数添加数据但无法这样做,似乎有些愚蠢的错误,请帮助我,我想用oops方式做,否则我可以做到

错误:解析错误:语法错误,意外'$ mysqli'(T_VARIABLE),期望第4行customer.php中的函数(T_FUNCTION)

的index.php

$adduser= new Userclass();
echo $adduser->addcutomer();

Db class.php

class db extends mysqli {
    private static $instance = null;
    private $user = "root";
    private $pass = "xxx";
    private $dbName = "xxx";
    private $dbHost = "localhost";

    public static function getInstance() {
    if (!self::$instance instanceof self) {
            self::$instance = new self;
    }
        return self::$instance;
    }
    public function __clone() {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
    public function __wakeup() {
        trigger_error('Deserializing is not allowed.', E_USER_ERROR);
    }

    private function __construct() {
        parent::__construct($this->dbHost, $this->user, $this->pass, $this->dbName);
        if (mysqli_connect_error()) {
            exit('Connect Error (' . mysqli_connect_errno() . ') '
                    . mysqli_connect_error());
        }
        parent::set_charset('utf-8');
   }

    public function dbquery($query)
    {
        if($this->query($query))
        {
            return true;
        }

    }
}

客户类

class Userclass extends db{
  $mysqli=db::getInstance();

  public function addcutomer(){

    $query = $mysqli->prepare("INSERT INTO `user` (`firstname`,`lastname`) VALUES (?, ?)");
    $col1 = 'abc';
    $col2 = 'def';
    $query->bind_param('ss', $col1, $col2);
    $query->execute();
    $query->close();
    return true;
  }

1 个答案:

答案 0 :(得分:-1)

试试这个,希望这项工作

$col1 = "abc";
$col2 = "def";