Hello, I was wondering if anyone could help me with some php code to pull stats from a mysql db. I wanted to show how many forums are hosted on my index page from this sql. $sql[] = " CREATE TABLE " . $accessname . "_forums ( forum_id smallint(5) UNSIGNED NOT NULL, cat_id mediumint(8) UNSIGNED NOT NULL, forum_name varchar(150), forum_desc text, forum_status tinyint(4) DEFAULT '0' NOT NULL, forum_order mediumint(8) UNSIGNED DEFAULT '1' NOT NULL, forum_posts mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, forum_topics mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, forum_last_post_id mediumint(8) UNSIGNED DEFAULT '0' NOT NULL, prune_next int(11), prune_enable tinyint(1) DEFAULT '0' NOT NULL, auth_view tinyint(2) DEFAULT '0' NOT NULL, auth_read tinyint(2) DEFAULT '0' NOT NULL, auth_post tinyint(2) DEFAULT '0' NOT NULL, auth_reply tinyint(2) DEFAULT '0' NOT NULL, auth_edit tinyint(2) DEFAULT '0' NOT NULL, auth_delete tinyint(2) DEFAULT '0' NOT NULL, auth_sticky tinyint(2) DEFAULT '0' NOT NULL, auth_announce tinyint(2) DEFAULT '0' NOT NULL, auth_vote tinyint(2) DEFAULT '0' NOT NULL, auth_pollcreate tinyint(2) DEFAULT '0' NOT NULL, auth_attachments tinyint(2) DEFAULT '0' NOT NULL, PRIMARY KEY (forum_id), KEY forums_order (forum_order), KEY cat_id (cat_id), KEY forum_last_post_id (forum_last_post_id) );"; Code (markup): Thanks for your time to look at this. Regards, Jay
Well once you connect to your db, it looks like you will just need an SQL statement like the following $sql[] = "SELECT COUNT(forum_id) FROM ". $accessname . "_forums"; PHP:
mysql_connect('localhost','username','password'); mysql_select_db('database_name'); $sql = "SELECT COUNT(forum_id) as fcount FROM ". $accessname . "_forums"; $res = mysql_query($sql); if($row = mysql_fetch_assoc($res)) { echo 'There are a total of '. $row['fcount'].' forums'; }else{ echo 'There was a problem retrieving the forum count'; } PHP: something like that (make sure you change the username, password, and database_name
Thanks but when I try the code i get. Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource Regards, Jay
mysql_connect('localhost','username','password'); mysql_select_db('database_name'); $sql = "SELECT COUNT(forum_id) as fcount FROM ". $accessname . "_forums"; $res = mysql_query($sql); if(!$res) { echo mysql_error(); }else{ if($row = mysql_fetch_assoc($res)) { echo 'There are a total of '. $row['fcoundt'].' forums'; }else{ echo 'There was a problem retrieving the forum count'; } } PHP: Tell me what error you get when you run this code
ah well there you have the problem. change your sql line to $sql = "SELECT COUNT(forum_id) as fcount FROM forums_table_name"; PHP: change forums_table_name to yours obviously