1

This is a clean installation and I was starting to develop a module.
So I have the following code (and nothing more apart from the hook_menu()):

$result = db_query("SELECT * FROM {variable}");
return db_affected_rows();

I used the variable table as an example since it was likely to be populated (and it is, of course!). Well, to my surprise db_affected_rows() always returns 0.
Is there any reason why it doesn't work? Are there any reported bugs that I may have missed?

Setup:

  • Drupal 6.22
  • PostgreSQL 8.4
  • PHP 5.3.8
4

To summarize for Drupal 6:

  • db_affected_rows() gives you number of rows returned by a select if the database is MySQL but not if it's PostgreSQL.
  • In Drupal 6 if you are using PostgreSQL you can run pg_num_rows() on the query to get the number of rows.

It is indeed database specific.

[Reference: this comment in the Drupal documentation]

  • Well, I don't know why but that's what I was expecting. So much for a database abstraction layer... Thank you. – acm Nov 24 '11 at 11:24
  • There are a several subtle difference like this between the MySQL and PostgreSQL versions. When you encounter weirdness like this, always read the entry and comments in the API section of the docs. – mpdonadio Nov 24 '11 at 13:32
  • @andrematos yeah :) Though they've updated the API for Drupal 7, see (drupal.stackexchange.com/questions/1287/…) for updates (merges) specifically :) – PleasantDemise Nov 29 '11 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.