How to know if there is a mysql syntax error from pdo

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • guillermobytes
    New Member
    • Jan 2010
    • 77

    How to know if there is a mysql syntax error from pdo

    Hi,
    i'm making a query with PDO and there is a SQL syntax error in it.

    Code:
    $sql = 'BAD CODE';
    $pdoStmt = $pdo->prepare($sql);
    if (false === $pdoStmt) {
    	echo 'ERROR';
    }
    it does not echo 'ERROR'

    any suggestions on how i should test a syntax error?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    if I remember right, PDO throws a PDOException in this case …
    Originally posted by php.net
    Return Values

    If the database server successfully prepares the statement, PDO::prepare() returns a PDOStatement object. If the database server cannot successfully prepare the statement, PDO::prepare() returns FALSE or emits PDOException (depending on error handling).

    Comment

    • guillermobytes
      New Member
      • Jan 2010
      • 77

      #3
      ok, wierd, because it didn't throw any Exception neither :S

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I suspect, "BAD CODE" isn’t enough to cause a MySQL error …

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          See PDO::errorInfo.

          Originally posted by Dormilich
          I suspect, "BAD CODE" isn’t enough to cause a MySQL error …
          It does.
          #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BAD CODE' at line 1

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            That's odd... The example in the manual, using the PDO::prepare method doesn't work, but if you substitute it for PDO::query, then it does...

            [code=php]$stmt = $dbh->query('bogus sql');
            if (!$stmt) {
            echo "\nPDO::errorIn fo():\n";
            print_r($dbh->errorInfo()) ;
            }[/code]
            [code=text]PDO::errorInfo( ):
            Array
            (
            [0] => 42000
            [1] => 1064
            [2] => You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'bogus sql' at line 1
            )[/code]

            Comment

            • Atli
              Recognized Expert Expert
              • Nov 2006
              • 5062

              #7
              Ahh Ok, you need to execute it and use the PDOStatement::e rrorInfo field.
              [code=php]$stmt = $dbh->prepare('bog us sql');
              $res = $stmt->execute();
              if (!$res) {
              echo "\nPDO::errorIn fo():\n";
              print_r($stmt->errorInfo()) ;
              }[/code]

              ... Not often that you see errors like that in the manual :|

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                What error?

                Comment

                • Atli
                  Recognized Expert Expert
                  • Nov 2006
                  • 5062

                  #9
                  Originally posted by Markus
                  What error?
                  According to example #1 for PDO::errorInfo, this:
                  [code=php]$stmt = $dbh->prepare('bog us sql');
                  if (!$stmt) {
                  echo "\nPDO::errorIn fo():\n";
                  print_r($dbh->errorInfo()) ;
                  }[/code]
                  Should produce something like:
                  Code:
                  PDO::errorInfo():
                  Array
                  (
                      [0] => HY000
                      [1] => 1
                      [2] => near "bogus": syntax error
                  )
                  However, according to my tests, it does not.
                  Granted, I tested this against a MySQL database rather than DB2, but PDO should act consistently despite which database you are using.

                  Comment

                  • Markus
                    Recognized Expert Expert
                    • Jun 2007
                    • 6092

                    #10
                    Oooh. I'll update the docs.

                    Comment

                    • guillermobytes
                      New Member
                      • Jan 2010
                      • 77

                      #11
                      ok thanks for the details

                      Comment

                      • Atli
                        Recognized Expert Expert
                        • Nov 2006
                        • 5062

                        #12
                        Originally posted by Markus
                        Oooh. I'll update the docs.
                        You can do that? :O
                        Nice :D

                        Comment

                        Working...