【问题标题】:Error in updating same value to database将相同的值更新到数据库时出错
【发布时间】: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。

【问题讨论】:

    标签: php mysql oop


    【解决方案1】:

    如果您的更新查询不影响任何行,则 ->exec($query) 的返回值为 0。 0false 在带有“!”的条件下相同检查。

    您可以在 if 条件中使用“=== false”:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-05
      • 2012-10-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多