2

Any idea how to fix this?

$dbo = JFactory::getDbo();
$query = 'SELECT `#__example`.`example_value` AS `value` FROM `#__example`';
$dbo->setQuery($query);
$rowsCount = $dbo->getNumRows();
$isEmpty = $rowsCount == 0;
echo $isEmpty;

I got

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, null given
in C:\server\www\libraries\joomla\database\driver\mysqli.php on line 343

and 1 in the response, while the table contains rows, so the result cannot be empty.

I just want to check whether the result set is empty without loading objects, etc... I load them in a later part of the code if it is not empty.

2

I found out that I need a $dbo->execute() before the $dbo->getNumRows(). It is interesting, since the $dbo->loadObjectList() does not require it.

Andy found the important part in the docs too: https://docs.joomla.org/Selecting_data_using_JDatabase#getNumRows.28.29

  • With loadObjectList() you can count the number of items in the returned array. Your query originally had no execute method, loadObjectList() is an execute method. Please mark your question as answered. – Terry Carter Mar 9 '17 at 19:21
  • That is as it is described in the docs at... docs.joomla.org/Selecting_data_using_JDatabase#getNumRows.28.29 – AndyGaskell Mar 9 '17 at 23:11
  • @AndyGaskell Thanks! I somehow missed that part. I thought if you have a select it is enough to use something result related, like loadObjectList() or getNumRows() to trigger execution. Apparently this is not always true. It would be nice to have a more consequent api. – inf3rno Mar 10 '17 at 2:04

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.