LIKE clause is not working in $wpdb->prepare() in wordpress

I am creating simple searching script with LIKE clause.
Below is the simple query with LIKE clause using php.

$rows = mysql_query("select * from description where tags like '%{$keyword}%'");

This above query work successfully. But LIKE clause not working with $wpdb->prepare(). Below is the code for that

Read More
$rows = $wpdb->get_results($wpdb->prepare("select * from description where tags like '%{%s}%'",$keyword));

What I am missing in this?

Related posts

3 comments

  1. try this:

    $param = "%{$keyword}%";
    $stmt = $db->prepare("SELECT * FROM description WHERE tags LIKE ?");
    $stmt->bind_param("s", $param);
    $stmt->execute();
    
    $result = $stmt->get_result();
    while ($row = $result->fetch_array(MYSQLI_NUM)) {
      foreach ($row as $r) {
        print "$r ";
      }
      print "n";
    }
    
  2. I usually use sprintf, and do something similar to this.

    global $wpdb;
    $querystr = sprintf(
    "SELECT * FROM description WHERE tags LIKE '%%%$s%%'"
    mysql_real_escape_string($s)
    )
    $rows = $wpdb->get_results($querystr, OBJECT);
    echo "</pre>"; print_r($rows); echo "</pre>";
    

Comments are closed.