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.

Read More
// 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?

Related posts

1 comment

  1. 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)
    );
    

Comments are closed.