2017-04-26 30 views
0

這是我使用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:形成execte功能。

回答

1

如果您的更新查詢不影響任何行,則 - > exec($ query)的返回值爲0. 0與條件爲「!」的條件相同false檢查。

您可以在您若條件中使用「===假」:

if(($response = $this->conn->exec($query)) === false){ 
+0

謝謝你的回答,這個** **解決了我的問題。 –