Reza Saadati
Reza Saadati

Reputation: 5419

prepared statement returns an empty array

I cannot use %s in my prepared statement.

echo $get_where; // returns: edited = 1
$get_uncontacted_members = $wpdb->get_results(
        $wpdb->prepare("SELECT * FROM yc_customers WHERE %s", $get_where)
);

This code returns an empty array. But when I use $get_where instead of %s (see code bellow), then it returns all the results from the database.

// This works
echo $get_where; // returns: edited = 1
$get_uncontacted_members = $wpdb->get_results(
        $wpdb->prepare("SELECT * FROM yc_customers WHERE edited = 1", $get_where)
);

Why wouldn't it work with %s?

Upvotes: 1

Views: 115

Answers (1)

Jonathan
Jonathan

Reputation: 2877

WordPress while uses the sprintf() syntax, it actually works like prepared statements. As such you can only pass the value of the column you are querying against, not entire column(s) and values.

$get_uncontacted_members = $wpdb->get_results(
        $wpdb->prepare("SELECT * FROM yc_customers WHERE IFNULL(edited,'') = %s", 1)
);

Upvotes: 1

Related Questions