Hi,
When I am using the PHP interface routines to Postgres, any error (like trying to fetch a non-existent row) will pop up as an error message in the web browser. Is it possible to catch this error before it shows up in the browser? I rather like the solution found in the MySQL interface routines, where you can use mysql_error() or mysql_errno() to get better control on how to manage error situations.

Egil

    Ejem, check out the manual.

    if you issue the functions with a leading '@'
    then it doesn't show the error mesg automatically. eg: @pg_connect(...)

    Then you have to test the variable;
    if (!$res=pg_query(..))
    then / mail yourself with the pg error function results on the body/

    Got it?

      Sorry, but this is half right and half wrong.

      Yes, you issue the calls to the built in pg_ functions with an @ at the front, no, you don't test the existence of a result code to see if it worked. Even on an error or a null set, you'll get a result handle.

      What you do, is much like with mysql, you check the error message, but in postgresql, you hand pg_errormessage the connection handle.

        You can suppress the errors with '@' in front of a pg_function. This makes it so that the error is not displayed and nothing more.

        I use this all the time for error-handling on a basic level.

        Example:

        $result = @pg_connect( $conn, $sql );

        if( !$result )
        {
        echo "Error: Connecting to database.";
        }

        You can take it to another level and do OOPHP and create classes to handle errors and their messages and do redirect or reconnections based on specific error types. The world is your oyster.

        Hope this helps.

          Scott, you're saying that Hunter's code won't work? Or did I get you wrong?

            No, sorry, my mistake. I just completely read yours wrong, and posted wrong too.

            Yes you check the result code for FALSE, I just usually do it a different way, I.e. if ($val==FALSE) {error handling here}

            Basically, the way you do it is:

            $host="hostname";
            $dbname="database";
            $user="myname";
            $password="mypassword";
            $conn = @pg_connect("host=$host dbname=$dbname user=$user password=$password");
            if ($conn==FALSE) die("Couldn't connect to database.");
            $query = "select * from sometable";
            $result = pg_exec($conn,$query);
            if ($result==FALSE){
            print @pg_errormessage($conn);
            mail("a message of doom");
            exit;
            }
            else {do something interesting}

            Note that any query that kills a backend will lose it's

              Thanks for a lot of useful help.
              Egil

                Write a Reply...