try ,catch with mysql_query

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

try ,catch with mysql_query

Post 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?
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: try ,catch with mysql_query

Post 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();
}
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: try ,catch with mysql_query

Post 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
Post Reply