Results 1 to 12 of 12
  1. #1
    Join Date
    Sep 2004
    Posts
    76

    total database records

    Hi,

    I am trying to display the total records in my database using this code, but it is not working:

    $getcount="SELECT COUNT(entryid) AS count FROM visitordata";
    $getcount2=mysql_query($getcount) or die("Could not get data");
    while(
    $getcount3=mysql_fetch_array($getcount2))
    {
    $getcount3[entryid]=strip_tags($getcount3[entryid]);
    echo
    "$getcount3[entryid]";
    }

    Does anyone have any idea why?

    Thanks

  2. #2
    Join Date
    Jul 2006
    Posts
    95
    have you connected to the server and got back the link ID for mysql_query? Probably look php.net and read some docs?

  3. #3
    Join Date
    Sep 2004
    Posts
    76
    Yes connected to the DB. I have other queries that work, but that one doesn't.

  4. #4
    Join Date
    Jul 2006
    Posts
    95
    why don't you use mysql_num_rows to get the total number of records?

  5. #5
    Join Date
    Sep 2004
    Posts
    76
    Ok, how do i do that? I am new to php and mysql.

  6. #6
    Join Date
    Jul 2006
    Posts
    95
    after sql_query just do
    PHP Code:
    $num_rows = @mysql_num_rows($getcount2); 

  7. #7
    Join Date
    Sep 2004
    Posts
    76
    ok i now have this:

    PHP Code:
    $getcount="SELECT COUNT(entryid) AS total FROM visitordata";
    $getcount2=mysql_query($getcount) or die("Could not get data");
    $num_rows = @mysql_num_rows($getcount2);
    while(
    $getcount3=mysql_fetch_array($getcount2))
    {
    $getcount3[entryid]=strip_tags($getcount3[entryid]);
    echo 
    "$num_rows";

    but it displays 1 (when there is more than 1 entry) and also displays the could not get data error message.

    Is my code still wrong?

  8. #8
    Join Date
    Mar 2006
    Posts
    965
    I believe what you're trying to accomplish is this:

    PHP Code:
    $getcount "

    SELECT COUNT(entryid) AS total 
    FROM visitordata

    "
    ;

    $getcount2 mysql_query($getcount) or die("Could not get data");
    $num_rows = @mysql_num_rows($getcount2);

    if (
    $num_rows 0) {

    while(
    $getcount3 mysql_fetch_row($getcount2)) {
    $getcount3['total'] = (isset($getcount3)) ? intval(trim($getcount3['total'])) : 0;
    }
    echo 
    $num_rows;


  9. #9
    Join Date
    Apr 2006
    Location
    Southern California
    Posts
    1,125
    your original should work, just echo $getcount3[count] instead of $getcount3[entryid],
    remember that you COUNT(entryid) AS count, there's only count, which return the number of rows, there are no entryid being returned.
    Sarcasm, the 6th Ponyman of the Apocalypse.
    Please do not inquire about Tim, the 5th Ponyman.

  10. #10
    Join Date
    Mar 2006
    Posts
    965
    Then it would seem my last post has been edited right before you submitted this input.

  11. #11
    Join Date
    Sep 2004
    Posts
    76
    Thank you all very much

  12. #12
    Join Date
    Aug 2005
    Location
    UK
    Posts
    654
    You should also use ''s on the colum name count,

    SELECT COUNT(entryid) AS 'count' FROM visitordata

    You might get away with it in this simple query but the parser will get confused if you do that sometimes, since count is a command.

    Best to get used to that stuff early..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •