www.webdeveloper.com
Results 1 to 4 of 4

Thread: Attempting to catch DB Exceptions with PHP

  1. #1
    Join Date
    Jul 2004
    Location
    Canada
    Posts
    88

    Attempting to catch DB Exceptions with PHP

    I am using PHP Version 5.1.4 and am trying to catch an exception if there are issues with the SQL query.

    Just like this :

    PHP Code:
    try {
           
    odbc_exec($DB,$sql);
          print(
    "<center><font color=\"green\">User Created</font></center>");
        }
    catch (
    RuntimeException $e) {
            print(
    "Exception cought");

    However it's not happening. I am still getting error :
    Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PK_logins'. Cannot insert duplicate key in object 'logins'., SQL state 23000 in SQLExecDirect in D:\WebRoot\Jupiter\admin\admin\addModifyDelete.php on line 72.

    Do I have to enable something in my ini file or do I have to declare that I am going to catch exceptions in this script or am I missing something simple?

    Thanks

  2. #2
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    no. php exceptions aren't as smart as other languages.

    PHP Code:
     try {
          
    $odbc_exec_res odbc_exec($DB,$sql);
    if(
    $odbc_exec_res){
          print(
    "<center><font color=\"green\">User Created</font></center>");
    }
    else{
    throw new 
    RuntimeException("Failed to connect.");
    }
        }
    catch (
    RuntimeException $e) {
            print(
    "Exception cought");

    also, you should always try to use exceptions w/ object oriented programming.
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

  3. #3
    Join Date
    Jul 2004
    Location
    Canada
    Posts
    88
    It does work, however I still get an error displayed

    Warning: odbc_exec() [function.odbc-exec]: SQL error: [Microsoft][ODBC SQL Server Driver][SQL Server]Violation of PRIMARY KEY constraint 'PK_logins'. Cannot insert duplicate key in object 'logins'., SQL state 23000 in SQLExecDirect in D:\WebRoot\Jupiter\admin\admin\addModifyDelete.php on line 73

    How can I make sure that this error doesn't appear providing that I still want PHP to report all errors?

    I thought if I'll catch an exception no errors are going to be displayed. I guess I was spoiled by JAVA

  4. #4
    Join Date
    Aug 2005
    Location
    The Garden State
    Posts
    5,634
    you have a catch 22 here:

    How can I make sure that this error doesn't appear providing that I still want PHP to report all errors?
    You can raise your error level w/ the error_reporting function, that should still catch errors like this, but then php won't "report" the error...
    Acceptable Use | SQL Forum FAQ | celery is tasteless | twitter

    celery is tasteless - currently needing some UI time

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles