If i use fetchCol(), I get results, but rowCount() returns 0.

$count = db_query("SELECT nid from {node} where title = :orgname and type = :type;", array (
						':orgname' => $org_name,
						':type' => $type,
						))->rowCount();

dpm ($count);

Is this a bug? or am I missing something?

Thanks!!!

Comments

Anonymous’s picture

Are you sure your values for the title and type should actually return anything? I copy and pasted this and added variables above for $org_name and $type, and it worked for me.

phen’s picture

Yes, it definitely returns results if I use the fetchCol() method.

In this case worked around it, not using rowCount(), but i continue to get this behavior with other completely different queries. It's not critical, so far I can work around it, but it's strange.

Any other ideas?

phen’s picture

Update:
RowCount() works on my production site but not on my local machine sandbox. So there must be some settings somewhere that disable it. Specs:
Production site:
MySQL 5.1.36
PHP 5.2.10

localhost:
MySQL 5.0.37
PHP 5.2.13

I wonder if upgrading MySQL on localhost would make a difference. It shouldn't, since it meets the minimum system requirements for D7. I don't think I have time to upgrade now, but I'll try it soon and report back.

Thanks for any input!

Damien Tournoud’s picture

Status: Active » Closed (works as designed)

PDOStatement::rowCount() is only guaranteed to return something for DELETE, INSERT, or UPDATE statements.

If you want to count a number of something, build a COUNT(*) query using SelectQuery::countQuery().

phen’s picture

Thanks for the help!

phen’s picture

Title: db_query result->rowCount() returns 0 even though there are results. » Correct documentation of db_query result->rowCount().
Component: database system » documentation
Category: support » task
Priority: Normal » Minor
Status: Closed (works as designed) » Active

The documentation at http://drupal.org/node/1251174 implies that result->rowCount() will work on a select query.

Can I suggest that the documentation be updated? I'd draft an edit myself but I'm not sure I understand the correct usage well enough yet...

More details:

The first line of the document is

A Select query will always return a result set object of zero or more records. There are several ways to then retrieve data from that result set, depending on the use case.

Then later, without indicating that a different type of query is required:

To count the number of Rows returned use:

$number_of_rows = $result->rowCount();

Thanks!

jhodgdon’s picture

Project: Drupal core » Documentation
Version: 7.7 »
Component: documentation » Correction/Clarification
Issue tags: +developer

That page is incorrect. The documentation of the rowCount() method (and the comment above) are correct:
http://api.drupal.org/api/drupal/includes--database--database.inc/functi...

So the page should be corrected. Moving to correct issue queue.

If you would like to help, just click "Edit" at the top of that page and fix it. Thanks!

kristiaanvandeneynde’s picture

Status: Active » Needs review

Done: http://drupal.org/node/1251174
Please review.

snufkin’s picture

Issue summary: View changes
Status: Needs review » Fixed

I think its clear now.

Status: Fixed » Closed (fixed)

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