Feb

11

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

Comments

Name (required)

Email (required)

Website

Speak your mind

Sponsors




Links