这是我使用 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 信息和错误 SQL: form execte 函数。
如果您的更新查询不影响任何行,则 ->exec($query) 的返回值为 0。与带有“!”的条件0
相同false
。检查。
您可以在 if 条件中使用“=== false”:
if(($response = $this->conn->exec($query)) === false){
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句