I did read the API; however, I still couldn't find how to handle the modifier (%s or %d) in this sql query's where clause. Where do you add the extra '%' signs?

This is not my actual query; however, it's simplicity makes a good example.

Normal where clause:
WHERE title LIKE '%My Title%'

===============================
Assign the variable:
$this_title = "My Title";

Use modifier %s for $this_title in the query.

$results = db_query("SELECT nid FROM {node} where type = 'articles' AND status=1 AND title LIKE '%%s%' ORDER BY title", $this_title);

===============================

Comments

nevets’s picture

I tend do something like

$results = db_query("SELECT nid FROM {node} where type = 'articles' AND status=1 AND title LIKE '%s' ORDER BY title", '%' . $this_title . '%');

I find this easier to read and comprehend compared to the alternative

$results = db_query("SELECT nid FROM {node} where type = 'articles' AND status=1 AND title LIKE '%%%s%%' ORDER BY title", $this_title);

(To get an actual % they need to be "doubled up")

Ashford’s picture

If there is a place in the Documentation How-To's to store this example, you need to add it. It's a good one.

Thanks for sharing your knowledge, nevets.