这是我使用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 ::错误信息和错误SQL:form execte function。
答案 0 :(得分:1)
如果您的更新查询不影响任何行,则返回 - &gt; exec($ query)为0。
“0
与”{1}}在条件为“!”时相同检查。
您可以在if条件中使用“=== false”:
false