Dear All,
Pls help. I am a newbie..I am getting an error like this "warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /srv/www/htdocs/Sathish/includes/common.inc(1645) : eval()'d code on line 45."
I am trying to select the data from the DB . At that time ,I am getting the above error. Thanks for any help/

Comments

dipen chaudhary’s picture

You'll need to use drupal's abstraction API

Instead of mysql_num_rows -> In drupal 6 u'll need to adapt your queries - http://drupal.org/node/114774#db-num-rows

Look at the all the database api here - http://api.drupal.org/api/group/database/6

Instead of mysql_query u'd use db_query and hence forth. Also if you can post your code here.

dipen chaudhary’s picture

Category: task » support
Sathishkumar-2’s picture

Yes. U r absolutely right dipen chaudhary!!! Thanks . Also I need to insert values into the external database from the story node . Should i do this using db_query itself or is there any other command for insertion?

yasir farooqui’s picture

As db_num_rows is not available in drupal 6 and also calling mysql_num_rows in drupal 6 some times gives the mentioned warning, so I just wrote a custom function for this and wanted to share here. Now rather than building your sql count query each time you need to count number of results, its better if you have some generic function, here is what I have written:

  function my_module_db_num_rows($sql) {
    $replace_from = substr($sql, strpos($sql, "SELECT") + strlen("SELECT"), strpos($sql, "FROM") - strlen("SELECT"));
    $replace_with = " COUNT(*) ";
    $sql_count = str_replace($replace_from, $replace_with, $sql);
    $args = func_get_args();
    //unset the first agrument in array of passed arguments as this will the sql query string
    unset($args[0]);
    return db_result(db_query($sql_count, $args));
  }

Note that this could have been accomplished by using regular expression to replace with the "Count(*)" string, but I used this because I am not too good in regular expressions :)

You will call this function as following:

  // for example
  $sql = "SELECT * FROM {node} WHERE nid = '%d'";
  $result = db_query($sql, 1);
  $num_rows = my_module_db_num_rows($sql, 1);
yasir farooqui’s picture

In fact I found the regular expression for this :) I found this in one of the drupal's include file: Here is the updated function:

  function my_module_db_num_rows($sql) {
    $sql_count = preg_replace(array('/SELECT.*?FROM /As'), array('SELECT COUNT(*) FROM '), $sql);
    $args = func_get_args();
    //unset the first agrument in array of passed arguments as this will the sql query string
   unset($args[0]);
    return db_result(db_query($sql_count, $args));
}
avpaderno’s picture

Status: Active » Fixed

http://drupal.org/update/modules/5/6#db-num-rows reports what to do, instead of using the function that has been removed.

I am marking this support request as fixed because it has been already replied from dipen chaudhary.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.