【发布时间】:2017-04-26 04:50:39
【问题描述】:
这是我使用 OOP 的数据库连接,请告诉我执行函数中出了什么问题,每当我使用相同的值进行更新时,它都会抛出 错误。
<?php
class db{
private $conn;
private $host;
private $user;
private $password;
private $dbname;
private $port;
private $debug;
function __construct($params=array())
{
$this->conn = false;
$this->host = "localhost";
$this->user = "root";
$this->password = "mysql";
$this->dbname = "icecreams";
$this->port = "";
$this->debug = true;
$this->connect();
}
function __destruct()
{
$this->disconnect();
// TODO: Implement __destruct() method.
}
function connect(){
if(!$this->conn){
try {
$this->conn = new PDO("mysql:host=$this->host;dbname=$this->dbname",$this->user,$this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND =>'SET NAMES utf8'));
}
catch (Exception $e){
die('Errer :'.$e->getMessage());
}
if(!$this->conn){
$this->status_fatal = true;
echo 'Connection BDD failed';
die();
}
else{
$this->status_fatal = false;
}
}
return $this->conn;
}
function disconnect(){
if($this->conn){
$this->conn = null;
}
}
function execute($query){
if(!$response = $this->conn->exec($query)){
echo 'PDO::errorInfo()';
echo '</br>';
echo 'error SQL:'.$query;
die();
}
return $response;
}}
如果我用不同的值更新它会更新,如果我用相同的值更新它会显示 PDO::error info 和 error SQL: form execte function。
【问题讨论】: