SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Zealot t0m|ta's Avatar
    Join Date
    Mar 2002
    Location
    Madrid - Spain
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Problems counting rows

    this is my first post so hello to all.

    Although i've been desiging sites for years i'm a total newbie with php/mysql, so if this is not the correct forum, please excuse me (and tell where to go.

    My database is webhosted using PHP Version 4.1.1 and Apache/1.3.19 (Unix.

    I'm trying to get the number of rows in a table I've used two methods but none of them work.

    1.- Using mysql_num_rows($result);

    It says the provided argument is not a SQL valid argument
    2.- Using Count(*)

    $num_rows = @mysql_query("SELECT COUNT(*) FROM Canales");
    echo("$num_rows");

    The weird result is Resource id #3


    It should be 14 as the table has 14 rows, the query on the database works out nicely.

    Any one can give me a hint? I'd be very gratefull indeed.

    Thanks in advance

    t0m|tamysql_num_rows($result);

  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One way:
    $sql = "SELECT COUNT(*) FROM Canales";
    $result = mysql_query($sql);

    $total = mysql_result($result, 0);

  3. #3
    SitePoint Zealot t0m|ta's Avatar
    Join Date
    Mar 2002
    Location
    Madrid - Spain
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile Thank you!!!!!

    It works!!

    thanks so much :-)

  4. #4
    SitePoint Addict psychedelic's Avatar
    Join Date
    Feb 2002
    Location
    Boston
    Posts
    284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    how do you do this with mySQL numrows?
    The Internet is prettier on a Mac.

  5. #5
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $sql = "SELECT aField FROM Canales";
    $result = mysql_query($sql);

    $total = mysql_num_rows($result);

  6. #6
    SitePoint Addict Clete2's Avatar
    Join Date
    Mar 2002
    Location
    Columbia, South Carolina
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    heh for yrs now i've been programming client-side stuff and apps... now i've gotten into server-side stuff as Perl, PHP, and MySQL (i think i'm enjoting server-side beter)
    My Blog
    My Photography Website
    Linux user #339185

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by psychedelic
    how do you do this with mySQL numrows?
    you don't. to simply count the number of rows in a table, always use SELECT COUNT(*) FROM ... as it's *VERY* fast.



    Originally posted by johnn
    $sql = "SELECT aField FROM Canales";
    $result = mysql_query($sql);

    $total = mysql_num_rows($result);
    do you have any idea how long that would take on a table with a million rows? generally you don't want to SELECT things that you aren't going to use. especially not just to count rows! on a million row table, SELECT COUNT(*) FROM ... will be just as fast as always (about 1/3 of a millisecond ).
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  8. #8
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    SELECT COUNT(aField) is even faster than SELECT COUNT(*) , isn't it?

  9. #9
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no. www.mysql.com/doc/G/r/Group_by_functions.html

    SELECT COUNT(*) doesn't actually count the rows (the count is maintained internally (for ISAM/MyISAM tables, anyway)), which makes it consistently extremely fast. SELECT COUNT(column) has to actually count each row (since it only counts non-null rows), which gets slower as the table gets bigger. and of course, actually retrieving the rows and then using mysql_num_rows() is the slowest, as i said.

  10. #10
    SitePoint Zealot t0m|ta's Avatar
    Join Date
    Mar 2002
    Location
    Madrid - Spain
    Posts
    118
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the tips, I'm learning fast, now I can get a list of the channels, my next step will be to be able to insert new ones XD

    Why didn't I start with hellow world ?


Bookmarks

Posting Permissions

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