Problems using db_query in a custom service

Events happening in the community are now at Drupal community events on www.drupal.org.
Posted by mottolini on June 1, 2011 at 2:55pm

It's quite weird, but maybe someone has already encountered the same problem.
The code, a service callback, is quite simple:

function myservice_service_search_products($query) {
$sql = "SELECT * FROM mv_products WHERE description LIKE '%". mysql_real_escape_string($query) ."%'";
$result = db_query($sql);

$products= array();
while ($product = db_fetch_object($result)) {
$products[] = $product;
}
return $products;
}

When I call it first time with a parameter (let's say "red"), it returns correctly the set of data. On subsequent calls with different parameters ("black", "blue" or whatever) it returns an empty dataset. If I call again with the first parameter ("red") I receive back again the correct dataset.
I checked this on two different systems (one Mac and one Linux, with different PHP/Mysql combination) with same results. So the error is sure mine, but I can't find it...
It seems like a handle has not been released and any call to that table works on the first dataset only...
Or maybe it's not possible to use db_query within a web service?
Other hint: of course querying the database directly with the SQL produces always the correct dataset.

Any help is appreciated...

Comments

db_query

Posted by itarato on June 1, 2011 at 3:13pm

Be careful with the DB API syntax:

<?php
$result
= db_query('SELECT * FROM {mv_products} WHERE description LIKE \'%%%s%%\'', $query);
?>

Thanks! You saved my life!

Posted by mottolini on June 1, 2011 at 3:32pm

Thanks!
You saved my life!

Services

Group organizers

Group categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds:

AltStyle によって変換されたページ (->オリジナル) /