我正在尝试使用绑定(bind)语句将数据插入表中。我所知道的是连接已建立,但准备语句不起作用。我做错了什么吗?
<?php
class connDb{
private $link;
private $host;
private $user;
private $pass;
private $dbname;
public function __construct($host, $user, $pass, $dbname){
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
$this->dbname=$dbname;
$this->link=mysqli_connect($this->host, $this->user, $this->pass, $this->dbname)
OR
die("Not connected to MySQL");
}
}
?>
<?php
$conn1=new connDb("localhost", "root", "", "dbind");
$xyz=$conn1->prepare("INSERT INTO table1 VALUES(?, ?, ?)");
$x="vikas";
$y="vikas@gmail.com";
$z="123n";
$xyz->bind_param('sss', $x, $y, $z);
$xyz->execute();
?>
最佳答案
您需要使用 MySQLi 连接而不是您自己的对象。
<?php
class connDb{
private $link;
private $host;
private $user;
private $pass;
private $dbname;
public function __construct($host, $user, $pass, $dbname){
$this->host=$host;
$this->user=$user;
$this->pass=$pass;
$this->dbname=$dbname;
$this->link=mysqli_connect($this->host, $this->user, $this->pass, $this->dbname)
OR
die("Not connected to MySQL");
}
public function getLink() {
return $this->link;
}
}
$conn1=new connDb("localhost", "root", "", "dbind");
$xyz=$conn1->getLink()->prepare("INSERT INTO table1 VALUES(?, ?, ?)");
$x="vikas";
$y="vikas@gmail.com";
$z="123n";
$xyz->bind_param('sss', $x, $y, $z);
$xyz->execute();
?>
关于php - 为什么prepare语句没有执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40155975/