Web Design and Web Development Forum

  1. #1
    darkecho's Avatar
    Join Date
    Aug 2005
    Location
    Michigan... USA
    Age
    23
    Posts
    2,337
    Rep Power
    9
  2. darkecho is on a distinguished road
  3. mysql_num_rows inside of while

    ive been trying this for a while and still cannot get it to work. I am trying to put $rows = mysql_num_rows($sql) inside a while loop...

    Basically
    $num_rows = array();
    $query = mysql_query("select * from sometable");
    while ($results = mysql_fetch_array($query)) {
    $sql = mysql_query("select * from somefield where somefield1 = somefield_query");
    $rows = mysql_num_rows($sql);
    $num_rows[] = $rows;
    }
    now when i do print_r($num_rows); the array is empty... so any help on getting this to work would be muchly appreciated
    Reply With Quote Reply With Quote
  4. #2
    Mitch's Avatar
    Join Date
    Dec 2004
    Location
    United Kingdom
    Age
    23
    Posts
    2,481
    Rep Power
    0
  5. Mitch will become famous soon enough Mitch will become famous soon enough
  6. Re: mysql_num_rows inside of while

    Try defining an integer variable ($x = 0; )

    use a loop, foreach() i think or something,

    $num_rows[$x] = $rows;
    Reply With Quote Reply With Quote
  7. #3
    Join Date
    Aug 2005
    Location
    Walton, New York
    Age
    23
    Posts
    138
    Rep Power
    7
  8. NedreN is on a distinguished road
  9. Re: mysql_num_rows inside of while

    PHP Code:
    $num_rows = array();
    $results mysql_query("SELECT * FROM `blah`");

       while (
    $row mysql_fetch_array($results)) {
          
    $nrRes mysql_query("SELECT * FROM `blah` WHERE `blah` = '$blah'");
          
    $totalRows mysql_num_rows($nrRes);
          
    $num_rows[] = $totalRows;
       } 
    I don't see why that wouldn't work... I haven't tested it, but I really don't see why it wouldn't.
    Reply With Quote Reply With Quote
  10. #4
    Join Date
    Sep 2005
    Posts
    5
    Rep Power
    0
  11. cached is on a distinguished road
  12. Re: mysql_num_rows inside of while

    Print the queries and then try some of them just to make sure its not the query thats bad because I see nothing wrong.
    Reply With Quote Reply With Quote
  13. #5
    darkecho's Avatar
    Join Date
    Aug 2005
    Location
    Michigan... USA
    Age
    23
    Posts
    2,337
    Rep Power
    9
  14. darkecho is on a distinguished road
  15. Re: mysql_num_rows inside of while

    thanks guys.. i figured it out.. i left somethin out now it works great. thnx guys..
    Reply With Quote Reply With Quote
  16. #6
    Atolar's Avatar
    Join Date
    Jun 2005
    Posts
    690
    Rep Power
    7
  17. Atolar is on a distinguished road
  18. Re: mysql_num_rows inside of while

    for reference for other coders, you can't count rows if what you're searching isn't an array.


    hense you need:
    PHP Code:
    $asdf mysql_query("select * from *");
    $asdf2 mysql_fetch_array($asdf);
    $asdf3 mysql_num_rows($asdf2);
    $asdf4 mysql_num_rows($asdf); // WILL NOT WORK 
    Reply With Quote Reply With Quote
  19. #7
    Join Date
    Jun 2005
    Location
    California, USA
    Age
    22
    Posts
    2,821
    Rep Power
    0
  20. Mau is on a distinguished road
  21. Re: mysql_num_rows inside of while

    Sorry, but I do this ALL the time with no problems what so ever:

    PHP Code:
    $result mysql_query("
    SELECT name
    FROM users
    "
    );
    print 
    mysql_num_rows($result); 
    Reply With Quote Reply With Quote
  22. #8
    darkecho's Avatar
    Join Date
    Aug 2005
    Location
    Michigan... USA
    Age
    23
    Posts
    2,337
    Rep Power
    9
  23. darkecho is on a distinguished road
  24. Re: mysql_num_rows inside of while

    i agree with Mau, I do stuff like that all the time, especially with the forum I am making.
    Reply With Quote Reply With Quote
  25. #9
    niallj's Avatar
    Join Date
    Jan 2005
    Location
    Manchester, UK
    Age
    22
    Posts
    612
    Rep Power
    8
  26. niallj is on a distinguished road
  27. Re: mysql_num_rows inside of while

    Me too- either that or 'SELECT COUNT(`whatever`) FROM ...'
    Reply With Quote Reply With Quote
  28. #10
    Join Date
    Oct 2004
    Location
    Middlesbrough, UK
    Age
    20
    Posts
    551
    Rep Power
    8
  29. php is on a distinguished road
  30. Re: mysql_num_rows inside of while

    hmm i think its best to use Maus solution..
    Reply With Quote Reply With Quote

Similar Threads

  1. Regex: Match text not inside
    By Mau in forum PHP Scripting
    Replies: 3
    Last Post: 08-16-2005, 06:24 PM