Want more features on Pastebin? Sign Up, it's FREE!
Guest

What is the most efficient way to rewrite this function

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 1.03 KB  |  views: 24  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. function get_total_adults()
  2. {
  3.     $sql = "SELECT SUM(number_adults_attending) as number_of_adults FROM is_nfo_rsvp";
  4.     $result = mysql_query($sql) or die(mysql_error());
  5.     $array = mysql_fetch_assoc($result);
  6.  
  7.     return $array['number_of_adults'];
  8. }
  9.        
  10. function get_total_adults() {
  11.     $sql = 'SELECT SUM(number_adults_attending) FROM is_nfo_rsvp';
  12.     $result = mysql_query($sql) or die(mysql_error());
  13.     // I'd throw a catchable exception (see below) rather than die with a MySQl error
  14.  
  15.     return mysql_result($result, 0);
  16. }
  17.        
  18. function get_total_adults() {
  19.     $sql = 'SELECT SUM(number_adults_attending) FROM is_nfo_rsvp';
  20.     $result = mysql_query($sql);
  21.     if (!$result) {
  22.         throw new Exception('Failed to get total number of adults attending');
  23.     }
  24.  
  25.     return mysql_result($result, 0);
  26. }
  27.  
  28. try {
  29.     $total_adults = get_total_adults();
  30. } catch(Exception $ex) {
  31.     die('Whoops! An error occurred: ' . $ex->getMessage());
  32.     // or even better, add to your template and dump the template at this point
  33. }
  34. // continue code
clone this paste RAW Paste Data