pull stats for php code

Discussion in 'PHP' started by boardster, Aug 31, 2008.

  1. #1
    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
     
    boardster, Aug 31, 2008 IP
  2. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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:
     
    JAY6390, Aug 31, 2008 IP
  3. boardster

    boardster Peon

    Messages:
    242
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Great thanks. How would i print the statement?

    echo?
    Code (markup):
    Regards,
    Jay
     
    boardster, Aug 31, 2008 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    JAY6390, Sep 1, 2008 IP
  5. Dreads

    Dreads Well-Known Member

    Messages:
    1,884
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    150
    #5
    also remember to use sql escape to prevent any injections
     
    Dreads, Sep 1, 2008 IP
  6. boardster

    boardster Peon

    Messages:
    242
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks but when I try the code i get.

    Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource

    Regards,
    Jay
     
    boardster, Sep 1, 2008 IP
  7. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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
     
    JAY6390, Sep 1, 2008 IP
  8. boardster

    boardster Peon

    Messages:
    242
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Now I get

    Table '._forums' doesn't exist
     
    boardster, Sep 1, 2008 IP
  9. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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
     
    JAY6390, Sep 1, 2008 IP
  10. boardster

    boardster Peon

    Messages:
    242
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #10
    ok thanks a lot. I will change it. :)
     
    boardster, Sep 1, 2008 IP
  11. lanmonkey

    lanmonkey Active Member

    Messages:
    549
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    60
    #11

    I second this.
     
    lanmonkey, Sep 1, 2008 IP