Total no of rows in MySQL database

Discussion in 'PHP' started by egdcltd, Nov 5, 2005.

  1. #1
    I want to output the number of entries in a MySQL database (rows), but to only count the rows that have been approved (approved=1). Can anyone tell me how to do this please?
     
    egdcltd, Nov 5, 2005 IP
  2. softplus

    softplus Peon

    Messages:
    79
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    select count(*) from mytablename where approved =1

    ?

    You might try expertsexchange (read the missing space after s, not before) for stuff like this, they're pretty good + quick.
     
    softplus, Nov 5, 2005 IP
  3. egdcltd

    egdcltd Peon

    Messages:
    691
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, now I've got my output, but I'm having trouble outputting it to the template. The page templates are stored in a MySQL database, and I want it to replace a string (#TOTAL#) with the variable ($totalarticles). I know I need to use str_replace, in a statement somewhat like
    str_replace("#TOTAL#", $totalarticles, );
    PHP:
    but I don't know what to use for the third variable, or how to get it outputted.
     
    egdcltd, Nov 5, 2005 IP
  4. dave487

    dave487 Peon

    Messages:
    701
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The function works like this:
    $name = str_replace("originaltext", "newtext", $variable);
    PHP:
    where you are changing originaltext to newtext in $variable

    Hope this helps!
     
    dave487, Nov 7, 2005 IP
  5. themlife

    themlife Peon

    Messages:
    105
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #5
    This is how I do it...
    First I create connect.php which connects to the db.

    <?
          $username = "username"; 
          $password = "password"; 
          $host = "localhost"; 
          $database = "dbname"; 
          $table = "tablename"; 
          mysql_connect($host,$username,$password) or die("Error connecting to Database!
        " . mysql_error());
          mysql_select_db($database) or die("Cannot select database!
        " . mysql_error());
    ?>
    PHP:
    Then refer to it when you want to count the total:

    <?php
          include('connect.php'); //The database connection file
            $result = mysql_query("SELECT * FROM tablename"); //Change the table name
          $num_rows = mysql_num_rows($result);
          echo "$num_rows"; //Display the total number of rows as HTML text
    ?>
    PHP:
     
    themlife, Nov 9, 2005 IP