4

It is my understanding that

$db = JFactory::getDbo();
$db->setQuery($sql);            
$db->execute();
$numRows = $db->getNumRows();

Does not work in Joomla 3.x, right? I get a "Mysql_num_rows() expects parameter 1 to be resource" error. The query runs fine if I don't call getNumRows(), and I have a try/catch block around the whole thing.

What are we supposed to use instead for say, an update query, where one can't just use loadRowList()?

3

[EDIT] Use the Get Affected Rows function for INSERT, UPDATE, REPLACE or DELETE queries in Joomla

$db->getAffectedRows();

.


--Below is included as the original answer but not really the most helpful answer

This has been answered on stackoverflow here: https://stackoverflow.com/questions/27968427/how-to-count-numbers-of-row-of-sql-query-in-joomla-and-display-it

In general, it looks like best you've got in 3.x is:

$result = $db->loadObjectList(); 
$numRows = count($result);
  • Like I say in the question, this will not work when doing an update or delete query? – Jimmy Nov 25 '15 at 14:40
  • have you tried $db->getAffectedRows()? That function is in the database driver in J3.x I have not tried it. – Lance Nov 25 '15 at 17:17
  • You're right, that does indeed look promising. I will try it soon. – Jimmy Nov 25 '15 at 22:43
  • $db->getAffectedRows() does work. If you update your answer to reflect that, I will mark it as the answer. Thanks! – Jimmy Nov 29 '15 at 23:02
2

getNumRows() is valid in Joomla 3.x.

As for your error... "getNumRows() will return the number of result rows found by the last query and waiting to be read. To get a result from getNumRows() you have to run it after the query and before you have retrieved any results." Basically, "if you run getNumRows() after loadRowList() - or any other retrieval method - you may get a PHP Warning"

https://docs.joomla.org/Selecting_data_using_JDatabase#getNumRows.28.29

  • I don't know if one was always supposed to use getAffectedRows() when doing an update query in previous versions of Joomla, but in Joomla 3.x, getNumRows() definitely does not work and getAffectedRows() definitely does work for update queries. – Jimmy Nov 29 '15 at 23:02
  • 1
    The usage is different, getNumRows() "Retrieves the number of rows from a result set. This command is only valid for statements like SELECT or SHOW that return an actual result set. To retrieve the number of rows affected by a INSERT, UPDATE, REPLACE or DELETE query, use getAffectedRows()." getAffectedRows() "Get the number of affected rows by the last INSERT, UPDATE, REPLACE or DELETE query associated with link_identifier." – Walt Sorensen Nov 29 '15 at 23:28
1

This works for me, in Joomla 3.7.5

$db->setQuery($query);
$db->execute();
echo $db->getNumRows();

However, if I added $result = $db->loadResult(); before the getNumRows() then I get the error the original poster asked about.

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.