ok i want to count the number of rows in a table and display the total.

This is the code i have so far

$result = mysql_query("SELECT COUNT(*) FROM table WHERE example ='0'");
$num_rows = mysql_fetch_row($result);
$num_rows = $num_rows; 

echo "There are ".$num_rows." rows.";

i seem to be getting the following error

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

    Try this, it should work...

    $result = mysql_query("SELECT * FROM table WHERE example ='0' ");
    $num_rows = mysql_num_rows($result);
    
    echo "There are ".$num_rows." rows."; 
    

      tried the above code and i still get this error

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

      I tried taking the WHERE out and it worked, bu this is no good to me as i only want to count the records WHERE example is 0 ?????

        The error message indicates that your query failed. First check if $result is false, and if so generate some debug info via mysql_error(). (If the table is actually named "table", you probably need to quote it with back-quotes.) Also, mysql_fetch_row() returns an array of result row fields.

        $sql = "SELECT COUNT(*) FROM `table` WHERE example ='0'";
        $result = mysql_query($sql) or die("Count query failed: " . mysql_error() . "<br />\n$sql");
        $num_rows = mysql_fetch_row($result);
        echo "There are " . $num_rows[0] . " rows.";
        

          no the table isnt called table i just put that in while posting on the forum

          i tried the code and now get this error

          Count query failed: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'read ='0'' at line 1
          SELECT COUNT(*) FROM messaging WHERE read ='0'

            i renamed the filed and its working now :-?

              TheStalker;10898720 wrote:

              i renamed the filed and its working now :-?

              That's because READ is one of the MySQL Reserved Words and thus you either needed to a) change it to avoid the use of a reserved words, or b) properly delimit the database/column/table/view/etc. name (e.g. by surrounding it with double quotes or backticks).

                Write a Reply...