2

As requested I reformat the question:

For the following code:

$newPDO=new PDO($DSN,$USER,$PASS);
$newPDO->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);
$SQL='SOME SQL STATEMENT MAYBE FAULTY';
try{
$Query=$newPDO->Prepare($SQL)
$Success=$Query->execute();
if(!$Success)
  echo('A');
}
catch(PDOException $e)
{
  echo('B');
}

Question is, is it possible to see 'A' printed? Will the answer varies for different type of $SQL like select or insert?

Original question:

  • First I set the attribute to PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION, so that, I think, when execute() encounters an error from the DB, it should throw an exception. Then I realized that if that's true, I may not even have to check the return of execute() as long as an exception thrown is the same as a 'false' returned. I just need to catch from outside. If nothing caught the query should be ok.
    But I am not sure about this as execute() does not throw exception according to the manual by default. I tried several operations like duplicate PK and unique constraint violation when I insert something. my assertion is supported: PDO will throw an exception and I can get the detaield error msg from DB. However I don't know whether this is a universal truth. Is it possible to get a false from execute(), and no PDOEXCEPTION thrown when I have set ERRMODE to maximum? *
jc W
  • 99
  • 1
  • 7
  • 1
    If you enable exceptions, then PDO throws exceptions on failure, it will not return false. there's no point in checking false, because excecution will jump to any relevant `catch` block immediately. – Marc B Aug 18 '16 at 15:21
  • 1
    that's expected behaviour but execute() seems not not always throw an exception. Its actually what this question is about. – Niksac Aug 18 '16 at 15:55

2 Answers2

0

Try this:

   try {
      //Initial query processing
      $execute = $query->execute()
      if (!$execute) {
        //catch the exception
        throw new Exception ("blah")
      }
    } catch (Exception $e) {
      //Exception logic here

    }

Write your queries like this & if they encounter an exception in the try section, it will relay to the catch section and you can handle it however you like (whether or not id depends on a certain case).

If there are no exceptions it will never reach the catch statement, and your query will just execute.

When it comes to DUPLICATES -> you need to work around this in SQL. For example your query could be :

INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b); 
mlishn
  • 1,689
  • 14
  • 19
  • 1
    Yes this is basically what I have used to test, and as I said I tried to insert duplicate pk etc. However I don't think I want to enumerate every cases that my query may cause an error, and frankly speaking I don't know all cases. Therefore I ask this question for a quick answer. I can find some posts very likely saying my assertion is true such as Bill Karwin's comments in http://stackoverflow.com/questions/8618618/php-pdo-mysql-transaction-code-structure and others, but like I said, I am not sure. – jc W Jul 24 '12 at 02:59
  • As long as you have the correct query, this will find all the errors. It just looks for when the query is not executed, whatever the case maybe. Duplicates should be handled in the SQL statment – mlishn Jul 24 '12 at 13:38
  • 1
    (1) yes that's what an error should be right? like your query is not actually executed. Now my point is that if every error will be handled by an exception thrown, the meaning of the return value is zero. Cos if I catch one, a false was returned (but the value won't be returned in reality as it already jumpped out). (2) You suggest I check every possible duplicate before doing an insertion? I can't see the point actually. why not just read the error msg from DB? It's just one DB operation and tells you all the info you need. Maybe I am wrong but hopefully someone clarify me. – jc W Aug 01 '12 at 08:49
  • no, I'm saying to write your queries like this & if they encounter an exception in the `try` section, it will relay to the `catch` section and you can handle it however you like (whether or not id depends on a certain case). If there are no exceptions it will never reach the `catch` statement, and your query will just execute. When it comes to **DUPLICATES** -> you need to work around this in SQL. For example your query could be : `INSERT INTO table (a,b,c) VALUES (1,2,3),(4,5,6) ON DUPLICATE KEY UPDATE c=VALUES(a)+VALUES(b);` – mlishn Aug 01 '12 at 11:48
  • Thanks. However, if I set PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION, duplicate PK will throw an exception. In addition, I think in many cases we just need to tell the user you entered something already there, like the username you trying to registered is used by others. As such, I put the check condition in the catch part. Again that's not my question. I am pretty sure dupliicate PK, foreign key constraint will throw an exception from PDO. However I see a bunch of SQL errors from mysql but I am not sure whether PDO can throw exception accordingly. – jc W Aug 02 '12 at 02:29
  • you need to format your questions better next time. Sorry I couldn't help – mlishn Aug 02 '12 at 03:29
  • @ milishn, Thank you anyway. I do a reformation as suggested. – jc W Aug 02 '12 at 06:53
0

I have seen execute() returning false without throwing an exception which is kind of unexpected / bad behaviour in my opinion. This means that we basically have to do both - error and exception handling in parallel.

In my case: if I prepared an insert query without any parameters and then executed with a parameter. Execute will not throw an exception but return false. I suggest anyone to use @mlishn's solution.

Niksac
  • 770
  • 1
  • 10
  • 21