displaying total records in mysql table

Discussion in 'PHP' started by Mr.Bill, Feb 9, 2008.

  1. #1
    How would I achieve this, I would like to display the current record count from a certain mysql table. in the footer of a php web page
     
    Mr.Bill, Feb 9, 2008 IP
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    the sql you need is : SELECT COUNT(*) FROM table;
    the rest you should be able to locate.
     
    shallowink, Feb 9, 2008 IP
  3. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #3
    I had part of that

    SELECT count( * ) as total_record FROM table

    Its the rest which is the problem :)
     
    Mr.Bill, Feb 9, 2008 IP
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    huh? connect to the db. make the query. catch the result, display

    here code I know works

    $result_count = mysql_query('SELECT count(*) FROM `TABLE`');
    $totalrows = mysql_result($result_count,0,"count(*)");
     
    shallowink, Feb 9, 2008 IP
  5. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #5
    Alright your explaining this as though I may have done this before. If you could please just give me the technical name for what I am trying to do I can read on it and figure it out. Just need a place to start. All my results yield the part I posted nothing for actually implementing into the code :)
     
    Mr.Bill, Feb 9, 2008 IP
  6. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #6
    Just cut n paste, what I posted above. Just figured the hard part was the SQL, my bad.
     
    shallowink, Feb 9, 2008 IP
  7. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #7
    Well I have a couple issues I try this

    <?php
    
    $username="username";
    $password="password";
    $database="database";
    
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    $result_count = mysql_query('SELECT count(*) FROM `subscribers`');
    $totalrows = mysql_result($result_count,0,"count(*)");
    
    mysql_close();
    
    ?>
    PHP:
    I dont get any errors but I get no results, Also is it possible so it uses the current config file for the password and username etc. so I dont have to have it here also
     
    Mr.Bill, Feb 9, 2008 IP
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    add:
    echo "<b>Record Count: $totalrows</b>";

    can go right before the mysql_close();
    change the b to whatever html code you want, just watch out if you use " inside the string.
    yes long as the user info is called prior to db use.
     
    shallowink, Feb 9, 2008 IP
  9. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #9
    So would it be called like this or another way?

    <?php
    
    require("config/config.php");
    
    mysql_connect(localhost,$username,$password);
    @mysql_select_db($database) or die( "Unable to select database");
    
    $result_count = mysql_query('SELECT count(*) FROM `subscribers`');
    $totalrows = mysql_result($result_count,0,"count(*)");
    
    echo "<b>Record Count: $totalrows</b>";
    
    mysql_close();
    
    ?>
    PHP:
     
    Mr.Bill, Feb 9, 2008 IP
  10. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #10
    Looks right to me, you run it yet?
     
    shallowink, Feb 9, 2008 IP
  11. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #11
    Yeah works the other way but when I try with

    require("config/config.php");

    I get the errors
    I know its there as the other pages can call it fine. Odd
     
    Mr.Bill, Feb 9, 2008 IP
  12. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #12
    path issue perhaps? is the test file above config/ ?
     
    shallowink, Feb 9, 2008 IP
  13. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #13
    public_html/subscribe.php (where I am adding the code)
    public_html/config/config.php
     
    Mr.Bill, Feb 9, 2008 IP
  14. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #14
    ok, try putting ./config/config.php
     
    shallowink, Feb 9, 2008 IP
    Mr.Bill likes this.
  15. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #15
    Thanks for all your help. That didnt do it but using it the other way works so should be fine


    Thank you again.
     
    Mr.Bill, Feb 9, 2008 IP
  16. Mr.Bill

    Mr.Bill Well-Known Member

    Messages:
    2,818
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    160
    #16
    Thank you again figured it out :)

    
    mysql_connect($db_addr, $db_user, $db_pass) or die(mysql_error());
    mysql_select_db($db_name) or die(mysql_error());
    
    
    PHP:
     
    Mr.Bill, Feb 9, 2008 IP
  17. mab

    mab Active Member

    Messages:
    563
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    80
    #17
    What about trying "mysql_num_rows" ?

    <?php
    
    $conn = mysql_connect("localhost", "mysql_user", "mysql_password");
    mysql_select_db("database", $conn);
    
    $result = mysql_query("SELECT * FROM table1", $conn);
    $num_rows = mysql_num_rows($result);
    
    echo "$num_rows Rows\n";
    
    ?>
    
    PHP:
     
    mab, Feb 10, 2008 IP