简体   繁体   中英

PHP PDO Wrapper class implementation

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?

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

You'll only receive an error if the SQL command itself is in error. So if Employes is a valid table, and CieNo, Nom, Dept, and EmplCode are all valid columns, then your SQL statement is valid, even though it doesn't always find a row to UPDATE. Technically, it was "successful", just not in the way you're wanting.

Take a look at rowCount()

for info on how to determine whether 0 or more rows were affected by your query.

In fact, this whole class appears to be utterly useless. Exactly the same outcome can be achieved with raw PDO, with very small addition.

$cn = new PDO(/*Connection string and params here*/);
$cn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$stmt = $cn->prepare($sql);
$stmt->execute($params);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM