mysql, classes, exceptions and error handling

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

This mysql_error doesnt give me an error below, but when I run it at the  
global scope it does give me errors.  I went ahead and checked for  
mysql_error(), but normally I dont have to, why?

   public function runQuery() {

     try {
       /// Use mysql_error() to check for errors
       if (!empty($this->build_query)) {
    $this->sql_result = mysql_query($this->build_query);
    $this->lastinsertid = mysql_insert_id();
    $err = trim(mysql_error());
    if (!empty($err)) {
      throw new Exception("".$err);
    } // end of if - check error

       } // end of if //

     } catch(Exception $e) {

       $this->error_message = "ERR: ".$e->getMessage();

       if ($this->verbose == 1)
    echo $this->error_message;

     } /// end of try - catch //

     return $this->error_message;
   } // end of the method //

Ramza from Atlanta

Re: mysql, classes, exceptions and error handling

Quoted text here. Click to load it
limited knowledge that the problem is related to the scope of your
mysql_* function rather that a problem with it. If you are not
declaring the connection object in your function, it will not be
available to give an error...
Remember that you must reference your connection object throughout your
For a similar class, I've done this:

//(connecting to mysql)
$this->dbConnection = mysql_connect($this->host, $this->user,

//Later on the "runQuery" function:
$this->sql_result = mysql_query($this->$sql,$this->dbConnection) ;

//Finally the error handling uses:

Hope this helps...


Re: mysql, classes, exceptions and error handling

Ramza Brown wrote:
Quoted text here. Click to load it

Well, let's see here.

First of all - you should check the response of mysql_query() (in  
$this->sql_result in your code) to determine if you have an error.  And  
you need to do that before you call mysql_insert_id().

Only call mysql_insert_id() if it is indeed an insert.

And you should ALWAYS check the response of ANY mysql call to see if it  

Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.

Site Timeline