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 求一下解题思路,完全不懂
  • ¥15 tensorflow
  • ¥15 densenet网络结构中,特征以cat方式复用后是怎么进行误差回传的
  • ¥15 STM32G471芯片spi设置了8位,总是发送16位
  • ¥15 R语言并行计算beta-NTI中tree文件的类型
  • ¥15 如何解读marsbar导出的ROI数据?
  • ¥20 求友友协助弄一下基于STC89C52单片机的声光控制灯原理图
  • ¥15 arduino双向交通灯设计
  • ¥15 有没有会粒子群算法的大能(○゜ε^○)求带不会出收敛图😭
  • ¥15 Matlab读取根元素出错