du5739 2014-05-30 14:48
浏览 59
已采纳

PHP PDO Wrapper类实现

I have been developing a wrapper class inside a small PHP framework and I am experiencing something weird with one of the method in my class.

First here is the class code :

<?php
namespace Framework;
header('Content-Type: text/html; charset=UTF-8');

class cConnexion 
{
    public $m_log;//This will be instantiate as a cLog object (another class in the namespace)
    private $m_DB;//PDO instance
    private $m_Host;//Host of the conneciton string
    private $m_DBName;//Name of the DB to connecte to
    private $m_Driver;//Driver name, mysql for MySQL, sqlsrv for MSSQL
    private $m_Login;//Username for authentification
    private $m_Password;//Password for authentification

    public function __construct($p_Driver = "mysql", $p_host, $p_DBName, $p_login, $p_password)
    {
        $this->m_Host= $p_host;
        $this->m_Driver = $p_Driver;
        $this->m_DBName = $p_DBName;
        $this->m_Login = $p_login;
        $this->m_Password = $p_password;
        $this->m_log = new cLog('', '', true, false, false);
    }

    public function SecureExecute($p_query, $p_param)
    {
        try
        {
            $stmt = $this->m_DB->prepare($p_query);
            $status = $stmt->execute($p_param);
            $this->m_log->setMessageFR("Aucune exception ne s'est levé. ");
            $this->m_log->setMessageEN("No exceptions were raised. ");
            $this->m_log->setSuccess($status);
            return $status;
        }
        catch(\PDOException $e)
        {
            $this->m_log->setMessageFR("Une exception de type PDO est levé. ".$e->getMessage());
            $this->m_log->setMessageEN("A PDO exception was raised. ".$e->getMessage());
            $this->m_log->setSuccess(false);
            return false;
        }
    }
}   
?>

Note that I have remove every other method that are not relevant to the question but kept the constructor and all the properties. Also note that there is a method to connect the $m_DB property to the DB, you might want to assume it has already been called.

Here is the problem I need help solving: Step by Step description of what is going on:

  1. I create an instance of cConnexion and use the connecteToDB method that isn't described in here but it is working properly because I can use another method to do MySQL 'SELECT' statement.
  2. I tried to UPDATE a row which doesn't exist in a MySQL table but the object still tell me that the UPDATE was successful.

Is that normal? I read the PDO::PDOStatement::execute Doc and is says that the return value of PDO::PDOStatement::execute is either true (on success) or false(if fails).

Here is an exemple of code using the method:

$sql = "UPDATE Employes SET CieNo = 3, Nom = :Name, Dept = :Department WHERE EmplCode = :Code";
$params = array
(
    'Code'=>$EmplCode,// = 123
    'Name'=>($lname." ".$fname),// = Lalonde Sebastien
    'Department'=>$dep,// = INFO
);
$cn = new cConnexion(/*Connection string and params here*/);
$cn->connectToDB();
if($cn->SecureExecute($sql, $params))
{
    echo "SQL1: true";
}

The following code always output "SQL1: true" even when the UPDATE shouldn't had work...(because the Employes table doesn't contains an EmplCode = 123). Can someone suggest a way to change my class so that the method SecureExecute() return false when this happen?

  • 写回答

3条回答 默认 最新

  • dqq46733 2014-05-30 14:59
    关注

    The execute method returns true on success, which in your case is true: The mysql query was executed successfully, however no rows were effected.

    If you need to check whether the update actually altered anything, you need to use "rowCount":

    $stmt = $this->m_DB->prepare('Update ...');
    $stmt->execute();
    
    $effected = $stmt->rowCount();
    

    You can then decide if you want to return a true/false value from your method.

    http://www.php.net/manual/en/pdostatement.rowcount.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀