2017-04-26 18 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と同じです。チェック。

あなたは、あなたの場合の条件に "=== false" を使用することができます:あなたの答えのための

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

感謝、この**解決**私の問題。 –

関連する問題