Feb
11
Proper Way to Use Joomla getNumRows
February 11, 2008 |
Joomla provides a sopisticated database abstraction layer to simplify the usage for 3PD. Joomla database class contains many methods for working with a query’s result set. Many people have encountered one problem when using getNumRows which is part of Joomla database class. The following chunk of code is the most common senario:
$db = JFactory::getDBO(); $query = "SELECT * FROM #__example2"; $db->setQuery($query); $rows = $db->getNumRows();
When you try to execute the above chuck of code, you will get an error message similar to the one below:
Warning: mysql_num_rows(): 108 is not a valid MySQL result resource in root:\mywebsite\libraries\joomla\database\database\mysql.php on line 123
The reason that this happens is because although we called setQuery, but we’ve forgotten to call $db->query(). Add $db->query() just after $db->setQuery($query); will solve the problem. So the following chunk of code should not return any error and works fine:
$db = JFactory::getDBO(); $query = "SELECT * FROM #__example2"; $db->setQuery($query); $db->query(); $rows = $db->getNumRows();
Similar Posts
- Way to use Joomla loadAssoc
- Joomla - Use loadObjectList and foreach to get list
- Install and Run Joomla on Ubuntu
- Joomla! - Make Latest News Module Display Date
- Joomla Retrieve Admin Password
- Remove Mootools From Joomla Header
- Joomla Manually Setup Enable SEF URL
- Joomla PDF Display Problem in IE7 Fix
- Joomla add Custom User Groups
- Joomla - Link Excerpt Article Title to Full Article
- Make Joomla localhost Email Work
- Joomla Loads Position Module within Content


















