I have to do a SQL question to a customized WordPress database desk the place sure columns are nullable.
I wrote this static technique:
public static perform does_name_exist( $identify ) {
world $wpdb;
$desk = $wpdb->prefix . self::DB_TABLE;
if ( is_string( $identify ) ) {
$question = $wpdb->put together( "SELECT `id` FROM `$table` WHERE `given_name` = %s", $identify );
} elseif ( is_array( $identify ) ) {
$question = $wpdb->put together(
"SELECT `id` FROM `$table` WHERE `name_prefix` = %s AND `given_name` = %s AND `additional_name` = %s AND `family_name` = %s AND `name_suffix` = %s",
empty( $identify['name_prefix'] ) ? null : $identify['name_prefix'],
empty( $identify['given_name'] ) ? null : $identify['given_name'],
empty( $identify['additional_name'] ) ? null : $identify['additional_name'],
empty( $identify['family_name'] ) ? null : $identify['family_name'],
empty( $identify['name_suffix'] ) ? null : $identify['name_suffix']
);
} else {
return false;
}
$id = $wpdb->get_var( $question );
if ( null !== $id ) {
return (int) $id;
} else {
return false;
}
}
The issue is when a worth in $identify array is empty the question examine NULL with equal as a substitute of IS operator.
Instance of flawed SQL question:
SELECT * FROM `wp_recipients` WHERE `name_prefix` = NULL AND `given_name` = 'John' AND `additional_name` = NULL AND `family_name` = 'Smith' AND `name_suffix` = NULL
Instance of appropriate SQL question:
SELECT * FROM `wp_recipients` WHERE `name_prefix` IS NULL AND `given_name` = 'John' AND `additional_name` IS NULL AND `family_name` = 'Smith' AND `name_suffix` IS NULL
How can I remedy the issue?