MyBB Community Forums

Full Version: How to call total functions from DB
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey,

I'm trying to learn a bit of MySQL and I can't seem to get the simplest function to work in MyBB.

Basically I have a table called 'codes' and i'd like to know how to display the total of that table in my footer. If somebody would be able to help me that would be awesome.
Wrong forum.

Try something like this;
$query = $db->query("SELECT COUNT(cid) AS total_cid FROM ".TABLE_PREFIX."codes");
$total_cids = $db->fetch_field($query, "total_cid");

Show the total_cids in footer template like this;
Total CIDs: {$total_cids}

In above code, I'm assuming "cids" as Increment value of your table.
(2012-07-10, 08:14 AM)Yaldaram Wrote: [ -> ]Wrong forum.

Try something like this;
$query = $db->query("SELECT COUNT(cid) AS total_cid FROM ".TABLE_PREFIX."codes");
$total_cids = $db->fetch_field($query, "total_cid");

Show the total_cids in footer template like this;
Total CIDs: {$total_cids}

In above code, I'm assuming "cids" as Increment value of your table.

Not 100% sure on the terminology used but if you mean the columbs in the table then I have 'code' 'type' 'id' 'password' 'codetitle' 'submitdate' and 'captcha.

But they don't always require a captcha or a password.

If I replace cids with id would that work then?

Also where do I place:

[quote='Yaldaram' pid='882425' dateline='1341908068']
Wrong forum.

Try something like this;
$query = $db->query("SELECT COUNT(cid) AS total_cid FROM ".TABLE_PREFIX."codes");
$total_cids = $db->fetch_field($query, "total_cid");
[quote]

Thanks
Do not create another thread. I've reported this thread so it will be move to correct section when staff visits to it.

And for the table, please take a screenshot of the table (in phpmyadmin) and attach it here so I could give the proper ID name.
(2012-07-10, 08:22 AM)Yaldaram Wrote: [ -> ]Do not create another thread. I've reported this thread so it will be move to correct section when staff visits to it.

And for the table, please take a screenshot of the table (in phpmyadmin) and attach it here so I could give the proper ID name.

Didn't realise it was in the wrong forum, but here's the screenshot:

[attachment=26793]
Right, its id
(2012-07-10, 08:33 AM)Yaldaram Wrote: [ -> ]Right, its id

Alright, so I've changed it to:

$query = $db->query("SELECT COUNT(id) AS total_cid FROM ".TABLE_PREFIX."codes");
$total_ids = $db->fetch_field($query, "total_id"); 

Would you be able to tell me where exactly to place that?

Sorry about all the questions,
The code should be run in ./global.php file just ABOVE the global_end hook. The call the variable in the footer template.
(2012-07-10, 09:35 AM)Yaldaram Wrote: [ -> ]The code should be run in ./global.php file just ABOVE the global_end hook. The call the variable in the footer template.

Did as you said but when I add in the code into the global.php it gives me this error:

MyBB SQL Error
MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1146 - Table 'hi.mybb_codes' doesn't exist
Query:
SELECT COUNT(id) AS total_id FROM mybb_codes


My DB is called hi but the table is just 'codes' and not 'hi.mybb_codes'.

Is there anyway to fix that?

I think it's because of the
".TABLE_PREFIX."codes"

Because that isn't the prefix.

Nevermind I just removed table prefix and left a . in it's place and it works perfectly.

Thanks again Yaldaram!