Advertisement
chrishajer

Query an external MySQL table

Oct 24th, 2012
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.52 KB | None | 0 0
  1. // http://www.gravityhelp.com/forums/topic/conditional-statement-to-check-game-status-and-week-using-database-in-wordpress
  2. add_filter('gform_notification_email_28', 'populate_admin_email_via_db', 10, 2);
  3. function populate_admin_email_via_db($email,$lead) {
  4.  
  5.     // set the email we're going to return equal to the email that was passed in, in case something fails
  6.     $selected_email = $email;
  7.  
  8.         // database connection details
  9.         $dbname  = 'databasename';
  10.         $dbuser  = 'databaseuser';
  11.         $dbpass  = 'databasepassword';
  12.         $dbhost  = 'databasehost';
  13.         $dbtable = 'customtablename';
  14.  
  15.         // the hidden accountID value from the form. it was item 4 in the $lead array for my form
  16.         $account = $lead[4];
  17.  
  18.         // establish the connection
  19.         $link = mysql_connect($dbhost, $dbuser, $dbpass);
  20.  
  21.     // if connection was established
  22.         if ($link){
  23.                 $sql = "SELECT user_email FROM $dbname.$dbtable WHERE user_login='$account' LIMIT 1;";
  24.                 $result = mysql_query($sql);
  25.  
  26.         // deal with the results
  27.                 if($result) {
  28.                         // the email address will be the one and only record returned
  29.                         $returned_record = mysql_fetch_row($result);
  30.             if(!empty($returned_record))
  31.                             $selected_email = $returned_record[0];
  32.                 }
  33.         // close the connection if it was established
  34.                 mysql_close($link);
  35.         }
  36.         return $selected_email;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement