Page 1 of 1

try ,catch with mysql_query

Posted: Mon May 12, 2008 11:55 am
by highjo
hello! trying to test an old code and it happens that forthe code bellow i'am having the expected if i input wrong data.

Code: Select all

 
if (!mysql_query($sql2,$con))
  {
    echo "There is a technical problem at this moment please contact live support" . mysql_error();
    exit();
  }
 
i'll like to catch it by doing this

Code: Select all

 
try
{
    mysql_query($sql2,$con);
}
 
catch(Exception $e)
  {
    echo "There is a technical problem at this moment please contact live support". $e->getMessage();
    
  }
 
it catching alright but not displaying the custom message.it is from a form with PHP_SELF as action so it just displaying the same page no error this time but no message either.I can't understand that.any idea?

Re: try ,catch with mysql_query

Posted: Mon May 12, 2008 12:26 pm
by John Cartwright
mysql_query() won't throw an exception, you need to do that yourself. Secondly, you need to pass an error message to the exception if you want to display it through $e->getMessage().

Code: Select all

try {
   if (!mysql_query($sql2,$con)) {
      throw new Exception("There is a technical problem at this moment please contact live support");
   } 
} catch(Exception $e) {
    echo $e->getMessage();
}

Re: try ,catch with mysql_query

Posted: Mon May 12, 2008 12:35 pm
by highjo
thanks about the tips.i think i've seen that method before and thought since mysql_query is not OOP it can't support rather this than try.thank a lot man