4

I have an update query

$query = $db->prepare("UPDATE user SET UserID='6',UserName='xyz' WHERE UserID= '6' "); $query->execute();

it runs fine, but when I change field UserName to UserNamee

$query = $db->prepare("UPDATE user SET UserID='6',UserNamee='xyz' WHERE UserID= '6' "); $query->execute();

It should show error, but it doesn't show any error

I just want to handle these kind of errors in my project.

Vishal Purohit
  • 261
  • 2
  • 12
  • 1
    http://stackoverflow.com/q/8992795/251311 – zerkms May 09 '13 at 07:13
  • 1
    If you don't catch errors you can't handle them, so you don't have the complete control of what's happening in your program. I don't advise you to do so. – Baronth May 09 '13 at 07:32
  • @Baronth this is not true. PHP has A LOT of ways to handle errors without catching them. And PHP does it way better than usual PHP user's approach. – Your Common Sense May 09 '13 at 07:35
  • @YourCommonSense Exceptions are used for exceptional events that changes or breaks the flow of the program execution. You can use if-else for sure, but ideally, as they are a "special event", they should be recognized and threated properly. You can use your own exceptions or use the default ones, but without catching them you doesn't have the complete control of them, they act just like a normal php error, and I don't like to have php errors in my code. – Baronth May 09 '13 at 08:18
  • @YourCommonSense Obvious you can setup error handling, but an exception is not a common error, PDO use them and we should use them in the best way, that isn't not catching them. – Baronth May 09 '13 at 08:20
  • @Baronth the code you posted is apparently not the best way. That's the point. Exceptions are great, but it doesn't mean you have to catch every one manually. Let PHP handle them - it can do it way better. If you don't like to have php errors in your code - **just tell PHP so.** That's way simpler than bloating your code with with tons of repeated useless try-catches. – Your Common Sense May 09 '13 at 08:37
  • @YourCommonSense It's obvious that he wont try-catch every function/method but he will surround only globally the flow of the program. And you can't tell PHP to "not have errors", you can just tell to "not show errors", that is hugely different (and completely wrong). With exceptions you can catch the error, eventually solve the problem, and continue with the flow of the program. – Baronth May 09 '13 at 08:46
  • @Baronth the code you posed doesn't solve any problem. Nor most of errors require such a solution. For some reason you are insisting on the solution which is good in your imagination but useless in the real code. The op asked how to have an error message, not how to handle an error. That's different matters. – Your Common Sense May 09 '13 at 08:51
  • @YourCommonSense The code I posted works and show the error message of the PDO Exception that PDO launch. – Baronth May 09 '13 at 09:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/29650/discussion-between-baronth-and-your-common-sense) – Baronth May 09 '13 at 09:00
  • @Baronth PHP will do it **already**. It doesn't need no your help with it. That's the point. Not to mention that echoing database errors out is HIGHLY INSECURE. Please, please. Why not to get some experience first? Experience with real code is worth 1000 discussions in chat. – Your Common Sense May 09 '13 at 09:09
  • @YourCommonSense Please please, that was an example. And exceptions doesn't stop the flow of the program, differently from normal php errors. By the way, let's stop this discussion here. Continue via chat or nothing, we should avoid discussions in comments. – Baronth May 09 '13 at 10:14
  • @Baronth WHY give a bad example which in many ways worse than current code the OP have? – Your Common Sense May 09 '13 at 10:17

4 Answers4

1

you can track the error in PDO using errorCode() function this function returns 0000 when no error else return a 4 digit number (error code), for your example you can try :

$query = $db->prepare("UPDATE user SET UserID='6',UserNamee='xyz' WHERE UserID= '6' ");         

$query->execute();

if($query->errorCode()=='0000')
{ echo 'no error'; }
else
{ echo 'error'; }
prashant
  • 1,012
  • 12
  • 21
0

PDO has various error modes that you can pass to the constructor as the driver_options argument. You can find them at http://php.net/manual/en/pdo.setattribute.php . Most people use array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION).

chx
  • 11,270
  • 7
  • 55
  • 129
0

You should use try-catch:

        try {

            $query = $db->prepare("UPDATE user SET UserID='6',UserName='xyz' WHERE UserID= '6' "); 
            $query->execute();

        } catch (PDOException $e) {
            echo $e->getMessage();
        }

And remember to do this after connection at the database:

$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
Baronth
  • 981
  • 7
  • 13
-1
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES,false);
        $query = $db->prepare("UPDATE user SET UserID='6',UserNamee='xyz' WHERE UserID= '6' "); 

        if (!$query) {
              print_r($db->errorInfo());
        }
else
    $query->execute();
Amir
  • 4,089
  • 4
  • 16
  • 28