Problems using db_query in a custom service

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
mottolini's picture

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

itarato's picture

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!

mottolini's picture

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: