How to display record total from a mysql database???

Discussion in 'PHP' started by netpox, May 6, 2008.

  1. #1
    How would i pull the total info of a record from my database and display that number on my website. Site is in php.

    Database name: database1
    Table: photos

    What code would i use to display the total number of photos from the photos table?

    is that enough info or do you need more info to get this done?
     
    netpox, May 6, 2008 IP
  2. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #2
    $a2 = mysql_query("select * from photos");
    $a = mysql_num_rows($a2);

    if you have a column where the photos name then your query will be

    $a2 = mysql_query("select * from photos where tableColumn != ''");
     
    bartolay13, May 6, 2008 IP
  3. netpox

    netpox Active Member

    Messages:
    1,625
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    90
    #3
    How would i display it on the page?
    Lets say i have 20,000 photos in my database and i want to display this message on my webpage:

    Photos: 20,000

    What exact code do i enter to my php page to display that?
     
    netpox, May 7, 2008 IP
  4. ggggqqqqihc

    ggggqqqqihc Peon

    Messages:
    92
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The better method is using MySQL function count(*)
    $rs = mysql_query("select count(*) from photos");
    $row = mysql_fetch_array($rs);
    $total = $row[0];
     
    ggggqqqqihc, May 7, 2008 IP
  5. netpox

    netpox Active Member

    Messages:
    1,625
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    90
    #5
    that isn't going to display the number! that is only pulling the info but not showing it in html
     
    netpox, May 7, 2008 IP
  6. netpox

    netpox Active Member

    Messages:
    1,625
    Likes Received:
    27
    Best Answers:
    0
    Trophy Points:
    90
    #6
    I found it. should be like this:

    <?php
    $result = mysql_query("SELECT * FROM photos"); //Change the table name
    $num_rows = mysql_num_rows($result);
    echo "$num_rows"; //Display the total number of rows as HTML text
    ?>
    <br>
     
    netpox, May 7, 2008 IP
  7. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #7
    -_- i already posted the answer
     
    bartolay13, May 7, 2008 IP
  8. swordbeta

    swordbeta Banned

    Messages:
    225
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yep, you did, but he wanted to echo it :p
    By the way, why does everyone use echo "$num_rows";?!
    It should be echo $num_rows; :p ( echo "Photos: ".$num_rows; )
     
    swordbeta, May 7, 2008 IP
  9. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #9
    oh i didnt notice the "echo" ahihihihi
    sorry for that

    yeah why putting the variable inside a ""
     
    bartolay13, May 7, 2008 IP
  10. mrphp

    mrphp Well-Known Member

    Messages:
    682
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    110
    #10
    for faster performance use this code

    $sql = mysql_query("select count(id) as total from table");
    $row = mysql_fetch_array($sql);
    echo $row['total'];

    Note that if you use select always use field name index to search for faster performance.
    Don't ever use * as your index.
     
    mrphp, Jun 21, 2008 IP