Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post

    Count number of entries in table

    PHP Code:
    //Count number of unaccepted reviews
    $result=mysql_query("SELECT * FROM comments");

    //Counter
    $i=0;
    while (
    $row mysql_fetch_array($result)) {
        
    $accept=$row['accepted'];
        if (
    $accepted == 0) {
            
    $i++;
        }

    I just tested the query by adding:
    PHP Code:
    if (!$result) { echo "Error"; } 

    Code:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/folder/public_html/admin/manage_reviews.php  on line 12
    I want it to count the number of rows which have the value of 0 in the accepted column.
    Last edited by martynball; 04-03-2010 at 07:47 PM.

  • #2
    Regular Coder
    Join Date
    Nov 2007
    Posts
    680
    Thanks
    319
    Thanked 1 Time in 1 Post
    Fixed. Forgot to put:
    PHP Code:
    mysql_select_db($dbname$con); 

  • #3
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Another solution, untested for syntax errors.

    PHP Code:
    $total_results mysql_result(mysql_query("SELECT COUNT(*) as Num FROM comments
    WHERE accepted = '0'"
    ),0); 


    ---------
    Leonard Whistler

  • #4
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts
    Could use this to count the results from the query.

    PHP Code:
    <?php
    $results 
    mysql_result(mysql_query("SELECT * FROM comments
    WHERE accepted = '0'"
    ));  
    $numrows mysql_num_rows($results);

    //just displays how many
    echo $numrows;
    /* use $numrows in the "if statement" */
    ?>


  •  

    Posting Permissions

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