PHP code in Form intro

dimoss

Well-Known Member
Hi

I am trying to include the following PHP script using Sourcener to get the total count of id's of a list
PHP:
<?php
 
$db = JFactory::getDBO();
 
$db->setQuery("SELECT COUNT('id') FROM 'fab_adeies'");
 
$number = $db->loadResult();
 
echo "The total number is: ".$number;
?>
However I can't get it work. It returns nothing instead of the number expected.

I tried also to put the same code in an J! article but again no number.

Any help?

Thanks
 
Finally found it. I made some changes in the code:

PHP:
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('COUNT(*)');
$query->from('fab_adeies');
$db->setQuery($query);
$number = $db->loadResult();
echo 'The total number is: '.$number;
?>
 
Cool. My next suggestion was going to be exactly that. Although it's sometimes easier to just write a simple string query, it's good to get in the habit of using the query builder API.

-- hugh
 
Hmmm, for some reason my previous post never made it, I think i was having connectivity issues yesterday. My first suggestion had been to assign an alias name to the count. I seem to recall that for some reason you have to name it when doing loadResult(), if you aren't using the query builder. So "SELECT COUNT(*) AS somename".

-- hugh
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top