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.

1 Answer 1

3

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

3
  • 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, 2017 at 19:21
  • That is as it is described in the docs at... docs.joomla.org/Selecting_data_using_JDatabase#getNumRows.28.29 Mar 9, 2017 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, 2017 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.