Support » Plugin: Allow Multiple Accounts » Fix PHP Notice: wpdb::prepare was called incorrectly

  • Starting with WordPress 4.8.3 a change was made to wpdb::prepare() to show a notice if the number of parameters didn’t match the number of placeholders in a SQL string.

    Notice: wpdb::prepare was called <strong>incorrectly</strong>. The query does not contain the correct number of placeholders (1) for the number of arguments passed (2). Please see <a href="https://codex.wordpress.org/Debugging_in_WordPress">Debugging in WordPress</a> for more information. (This message was added in version 4.8.3.)

    The fix:

    In allow-multiple-accounts.php, replace the count_multiple_accounts() function, lines 512 – 525:

    	public function count_multiple_accounts( $email, $user_id = null ) {
    		global $wpdb;
    
    		if ( $user_id && is_object( $user_id ) ) {
    			$user_id = $user_id->ID;
    		}
    
    		$sql = "SELECT COUNT(*) AS count FROM $wpdb->users WHERE user_email = %s";
    		if ( $user_id ) {
    			$sql .= ' AND ID != %d';
    		}
    		$count = (int) $wpdb->get_var( $wpdb->prepare( $sql, $email, $user_id ) );
    
    		return $count;
    	}

    with

    	public function count_multiple_accounts( $email, $user_id = null ) {
    		global $wpdb;
    
    		if ( $user_id && is_object( $user_id ) ) {
    			$user_id = $user_id->ID;
    		}
    
    		$sql = "SELECT COUNT(*) AS count FROM $wpdb->users WHERE user_email = %s";
    		$params = array( $email );
    
    		if ( $user_id ) {
    			$sql .= ' AND ID != %d';
    			$params[] = $user_id;
    		}
    
    		$count = (int) $wpdb->get_var( $wpdb->prepare( $sql, $params ) );
    
    		return $count;
    	}
    • This topic was modified 3 years, 5 months ago by Enchiridion.
  • The topic ‘Fix PHP Notice: wpdb::prepare was called incorrectly’ is closed to new replies.