mysql, classes, exceptions and error handling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ramza Brown

    mysql, classes, exceptions and error handling


    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($th is->build_query) ;

    $this->lastinsertid = mysql_insert_id ();
    $err = trim(mysql_erro r());
    if (!empty($err)) {
    throw new Exception("".$e rr);
    } // end of if - check error

    } // end of if //

    } catch(Exception $e) {

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

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

    } /// end of try - catch //

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


    --
    Ramza from Atlanta

  • bugzstuff@gmail.com

    #2
    Re: mysql, classes, exceptions and error handling

    >From your post, I presume you must be using classes... It seems from my
    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
    class..
    For a similar class, I've done this:



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

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



    //Finally the error handling uses:
    mysql_error($th is->dbConnection );



    Hope this helps...


    --
    Bugz


    Comment

    • Jerry Stuckle

      #3
      Re: mysql, classes, exceptions and error handling

      Ramza Brown wrote:[color=blue]
      >
      > 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($th is->build_query) ;
      >
      > $this->lastinsertid = mysql_insert_id ();
      > $err = trim(mysql_erro r());
      > if (!empty($err)) {
      > throw new Exception("".$e rr);
      > } // end of if - check error
      >
      > } // end of if //
      >
      > } catch(Exception $e) {
      >
      > $this->error_messag e = "ERR: ".$e->getMessage() ;
      >
      > if ($this->verbose == 1)
      > echo $this->error_messag e;
      >
      > } /// end of try - catch //
      >
      > return $this->error_messag e;
      > } // end of the method //
      >
      >[/color]

      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
      worked!

      --
      =============== ===
      Remove the "x" from my email address
      Jerry Stuckle
      JDS Computer Training Corp.
      jstucklex@attgl obal.net
      =============== ===

      Comment

      Working...