I want to display how many records are in a mysql table. I found a post that is related and got to this point

<?php
include("config.inc.php");	
	mysql_connect(localhost,$username,$password,$dbname);
mysql_select_db($dbname) or die( "Unable to select database");
	$sql = "SELECT COUNT(*) FROM `Signatures`;
$result = mysql_query($sql) or die("Count query failed: " . mysql_error(); 
$num_rows = mysql_fetch_row($result); 
echo "So far we have " . $num_rows[0] . " rows."; signatures
?>

I inserted the code on a html page in a text section of the page. I dont get an error, but I dont get the count either.
What do I need to do?

    You have so many syntax errors in that code.
    And other issues, too.
    If you would use error_reporting
    you would get messages about it.

    I have corrected those errors I could find.
    Looks like this now:

    <?php
    
    error_reporting(E_ALL);    // Get debug information
    ini_set('display_errors',1);
    
    include("config.inc.php");    
    mysql_connect('localhost',$username,$password); mysql_select_db($dbname) or die( "Unable to select database"); $sql = "SELECT COUNT(*) FROM `Signatures`"; $result = mysql_query($sql) or die("Count query failed: " . mysql_error()); $num_rows = mysql_fetch_row($result); echo "So far we have " . $num_rows[0] . " rows in signatures"; ?>

      Use code above and still no error, buy no count eigther

        No errors.
        At least we know there is no syntax errors.
        Is this what you get:

        So far we have  rows in signatures

        We can use [man]print_r[/man] to see what $num_rows holds.
        We can also add some more mysql testing.

        <?php
        
        error_reporting(E_ALL);    // Get debug information
        ini_set('display_errors',1);
        
        include("config.inc.php");    
        mysql_connect('localhost',$username,$password); mysql_select_db($dbname) or die( "Unable to select database"); $sql = "SELECT COUNT(*) FROM `Signatures`"; $result = mysql_query($sql); if(!$result){ echo "Count query failed: ".mysql_error(); } $num_rows = mysql_fetch_row($result); if(!$num_rows){ echo 'Num_rows is FALSE. '.mysql_error(); } echo "So far we have " . $num_rows[0] . " rows in signatures"; echo '<pre>'.print_r($num_rows, true).'</pre>'; // dump $num_rows ?>

          This is the code for the post above.

          
          <?php
             include("config.inc.php");
             mysql_connect(localhost,$username,$password,$dbname);
             mysql_select_db($dbname) or die( "Unable to select database");
             $sql = "SELECT COUNT(*) FROM `Signatures`;
             $result = mysql_query($sql) or die("Count query failed: " . mysql_error();
             $num_rows = mysql_num_rows($result);
             echo "So far we have " . $num_rows . " rows."; signatures
          ?> 
          
          
            jt_graphic;10939673 wrote:

            This is the code for the post above.

               $sql = "SELECT COUNT(*) FROM `Signatures`;
               $result = mysql_query($sql) or die("Count query failed: " . mysql_error();
               $num_rows = mysql_num_rows($result);
            

            $num_rows will always be 1 in this case, since count(*) returns one row containing the amount of rows in the table Signatures.

              johanafm;10939674 wrote:

              $num_rows will always be 1 in this case, since count() returns one row containing the amount of rows in the table Signatures.


              So, jt_graphics example
              should better be

                 $sql = "SELECT * FROM `Signatures`";
                 $result = mysql_query($sql) or die("Count query failed: " . mysql_error();
                 $num_rows = mysql_num_rows($result);
                 echo "So far we have " . $num_rows. " rows in signatures";
              

              The drawback can be on a very large table all rows are returned.
              This would take a lot of memory and maybe take long time.

              While using "SELECT COUNT(*) FROM Signatures"
              will only return one row in result.
              I would go for this.

                A quicker way to do this, maybe to combine the two statements

                $count = mysql_num_rows(mysql_query("SELECT * FROM `table` WHERE id > 0"));

                In case your too lazy to write it all out

                  It's still a bad idea. The only thing you keep is the number of rows, so there is absolutely no point in querying for actual table data. Just use SELECT count(*) FROM table to begin with.

                    halojoy;10939671 wrote:

                    No errors.
                    At least we know there is no syntax errors.
                    Is this what you get:

                    So far we have  rows in signatures

                    We can use [man]print_r[/man] to see what $num_rows holds.
                    We can also add some more mysql testing.

                    <?php
                    
                    error_reporting(E_ALL);    // Get debug information
                    ini_set('display_errors',1);
                    
                    include("config.inc.php");    
                    mysql_connect('localhost',$username,$password); mysql_select_db($dbname) or die( "Unable to select database"); $sql = "SELECT COUNT(*) FROM `Signatures`"; $result = mysql_query($sql); if(!$result){ echo "Count query failed: ".mysql_error(); } $num_rows = mysql_fetch_row($result); if(!$num_rows){ echo 'Num_rows is FALSE. '.mysql_error(); } echo "So far we have " . $num_rows[0] . " rows in signatures"; echo '<pre>'.print_r($num_rows, true).'</pre>'; // dump $num_rows ?>

                    I dont get anything between the <?php and ?> its like I dont even have the snipet of code in there

                      big-dog;10939717 wrote:

                      I dont get anything between the <?php and ?> its like I dont even have the snipet of code in there

                      🙂 Maybe you have no code!
                      Suppose the script ends in include("config.inc.php");
                      Are you sure the file 'config.inc.php' is working alright??

                      We can make a Test.
                      Try to put in this line after include("config.inc.php");

                      include("config.inc.php"); 
                      echo 'Test1'; // testing point1   
                      mysql_connect('localhost',$username,$password);

                        Try turning display_errors on and setting error_reporting to E_ALL.

                        Note that you have to do this outside of any PHP script (e.g. in a .htaccess file or in your php.ini file); if there is a syntax error in your code, using ini_set() and error_reporting() won't do anything since the code never gets parsed and executed.

                          I changed the config.ini.php file and on a differant page that also uses the file I got errors. Here is a couple of line above code and a couple below.

                          </tr>
                          <tr><td colspan="4"> 
                          	In the effert to improve things we need your help.
                          
                          <?php 
                          
                          error_reporting(E_ALL);    // Get debug information 
                          ini_set('display_errors',1); 
                          
                          include("config.inc.php");
                          echo 'Test1'; // testing point1
                          mysql_connect('localhost',$username,$password); 
                          mysql_select_db($dbname) or die( "Unable to select database"); 
                          
                          $sql = "SELECT COUNT(*) FROM `Signatures`"; 
                          $result = mysql_query($sql); 
                          if(!$result){ 
                              echo "Count query failed: ".mysql_error(); 
                          } 
                          
                          $num_rows = mysql_fetch_row($result); 
                          if(!$num_rows){ 
                              echo 'Num_rows is FALSE. '.mysql_error(); 
                          } 
                          
                          echo "So far we have " . $num_rows[0] . " rows in signatures"; 
                          
                          ?>
                          </td>
                          </tr>
                          <tr><td colspan="4"> 
                          	<p align="center"><b>Petition for Lighting</b></td>

                            I see you've got some test/debug echo's in there; are you still not seeing anything at all? If you're viewing the page in a browser, try viewing the source of the page to see what was actually sent to the browser (e.g. look at the HTML source, not the browser's rendered version).

                            If you still don't see anything - even the test echo's, then you've still got some kind of parse and/or fatal error somewhere in the code, and my post above still applies.

                              I changed the file extension from .html to .php and it is working. Dont really understand why Ive got other .html that has php/sql code and it works.

                                Nice it works 🙂

                                Any page to perform PHP must be xxxxx.php
                                (unless there is set .html to execute phpcode (AddType setting in Apache httpd.conf)

                                There is one exception to this,
                                you can include a file 'xxxxx.html' or 'xxxxx.htm' or 'xxxxx.txt' with phpcode inside
                                But the main page must be 'xxxxx.php' to execute phpcode

                                  Ok
                                  Thanks everyone for the help

                                    Write a Reply...