1

I have a database that I've build using PDO, and I am building some database tests using phpunit. However, I need to test some cases involved with pdo exec(), that returns a false or an int. What I want to know is: In which case pdo exec() will return false?

For example: I have this method that creates my database table. When my pdo exec() will be false?

public function createTable(): \PDO
    {
        try {
            $createTable = "
                            CREATE TABLE IF NOT EXISTS Transactions(
                                idTransaction int NOT NULL PRIMARY KEY AUTO_INCREMENT,
                                description varchar(255) NOT NULL,
                                price float NOT NULL,
                                category varchar(45) NOT NULL,
                                date varchar(10) NOT NULL,
                                type TINYINT NOT NULL);";

            $this->pdo->exec($createTable);
            return $this->pdo;
        } catch (\PDOException $PDOException) {
            throw new \PDOException($PDOException->getMessage());
        }
    }
izmatlDev
  • 13
  • 4
  • 1
    Well, it will return something that evaluates to `false` (not always directly `false`) if the statement fails. – arkascha Jan 09 '23 at 12:19
  • 1
    From the Manual ___PDO::exec() returns the number of rows that were modified or deleted by the SQL statement you issued. If no rows were affected, PDO::exec() returns 0.___ Odd place to start looking I realise :) – RiggsFolly Jan 09 '23 at 12:21
  • And ___Warning This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.___ – RiggsFolly Jan 09 '23 at 12:27
  • And finally: You normally get a `0 Rows affected` message from a table create query. Hence 0 or false – RiggsFolly Jan 09 '23 at 12:30
  • @arkascha what can be evaluated as false for example? – izmatlDev Jan 09 '23 at 13:02
  • Everything that PHP evaluates as `false` in a condition. That is documented, actually. Things like an empty string, the number 0, the boolean value `false`, ... – arkascha Jan 09 '23 at 16:17

1 Answers1

1

If PDO connection was not configured with param PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION (which is default behavior), exec() method will return false instead of throwing a PDOException. See this answer https://stackoverflow.com/a/8992933/6051839. In this case the last error happened will be accessible via PDO::errorCode() and PDO::errorInfo() methods, see this example https://www.php.net/manual/en/pdo.errorcode.php#refsect1-pdo.errorcode-examples.

So, to get false from exec(), just remove setting PDO::ATTR_ERRMODE attribute (if you set it now) and try to execute invalid sql, for example.

P.S. And as other commenters said, always check the result via strict comparison, since it can be 0 (which means no error, just no rows affected by query). I.e.

NOT

$result = $pdo->exec(...);
if (!$result) {// error!}

BUT

$result = $pdo->exec(...);
if ($result === false) {// error!}
Ilia Yatsenko
  • 779
  • 4
  • 7