Support » Plugins and Hacks » Hacks » Using $wpdb->get_results causes page to die

  • Resolved strictlyjc

    (@strictlyjc)


    Hello,

    I am working on a registration form and I want to restrict each email to be registered just once. It’s working perfectly on my localhost but on the remote site, the registration page dies when it is loaded.

    The initial code, which allowed multiple registration, worked both locally and remotely:

    if (!empty($_POST['email'])) {
    	$email = $_POST['email'];
    } else {
    	$error .= 'Please give us your email so we can contact you if we need to<br />';
    }

    But it acts up remotely once I add the database call. Here’s the other code:

    if (!empty($_POST['email'])) {
    	global $wpdb;
    	/* wpdb class should not be called directly.global $wpdb variable is an instantiation of the class already set up to talk to the WordPress database */
    	$result = $wpdb->get_results( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = 'email'" );
    	foreach ( $result as $row ){
    		if(stristr($row->meta_value, $_POST['email'])){
    			goto a;
    		} else {
    			$email = $_POST['email'];
    		}
    	}
    	a:
    	$error .= 'The email address: <span>' . $_POST['email'] . '</span> has already been registered.<br />';
    } else {
    	$error .= 'Please give us your email so we can contact you if we need to<br />';
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    ‘goto’ is a relatively recent operator for php, your hosted version is probably not running a very recent version. Restructure your loop so goto is not required and all should be OK.

    FWIW, many programmers feel goto is a hack construct and does not belong in any language. Admittedly, it can be quite handy sometimes, I personally avoid using it unless I’m really stuck.

    Thanks bcworkz! I realised I really didn’t need the ‘goto’. Once I took it out and inserted the error directly it worked perfectly!

    Cheers!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Using $wpdb->get_results causes page to die’ is closed to new replies.