Re: mysql_num_rows()

From: Date: Tue, 22 Feb 2011 14:12:08 +0000
Subject: Re: mysql_num_rows()
References: 1 2 3  Groups: php.general 
Request: Send a blank email to php-general+get-311521@lists.php.net to get a copy of this message
On 22/02/11 13:59, Gary wrote:
"Pete Ford"<pete@justcroft.com> wrote in message news:A4.C0.39221.B3CA36D4@pb1.pair.com...
On 22/02/11 05:40, Gary wrote:
Can someone tell me why this is not working? I do not get an error message, the results are called and echo'd to screen, the count does not work, I get a 0 for a result... $result = mysql_query("SELECT * FROM counties WHERE name = 'checked'") or die(mysql_error()); if ( isset($_POST['submit']) ) { for($i=1; $i<=$_POST['counties']; $i++) { if ( isset($_POST["county$i"] ) ) { echo "You have chosen ". $_POST["county$i"]."<br/>";
          }
      }
} $county_total=mysql_num_rows($result); while($row = mysql_fetch_array($result)) {$i++;
      echo $row['name'];
} $county_total=mysql_num_rows($result); echo "$county_total"; echo 'You Have ' . "$county_total" . ' Counties '; ?>
The first thing I see is that you do $county_total=mysql_num_rows($result) twice. Now I'm not to sure, but it is possible that looping over mysql_fetch_array($result) moves an array pointer so the count is not valid after the operation... that's a long shot. But since you are looping over the database records anyway, why not just count them as you go? Isn't $i the number of counties after all the looping? --
Peter Ford, Developer                 phone: 01580 893333 fax: 01580
893399 Justcroft International Ltd. www.justcroft.com Justcroft House, High Street, Staplehurst, Kent TN12 0AH United Kingdom Registered in England and Wales: 2297906 Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS
Peter Thank you for your reply. I did notice that I had the $county_total=mysql_num_rows($result) twice. I had moved and removed it, always getting either a 0 or 1 result. I put up another test page with $result = mysql_query("SELECT * FROM counties ") or die(mysql_error()); $tot=mysql_num_rows($result); echo "$tot"; ?> And it worked fine. I have tried count() as well as mysql_num_rows() but am getting the same result. Could you explain what you mean by count them as I go? Again, Thank you for your reply. Gary __________ Information from ESET Smart Security, version of virus signature database 5895 (20110222) __________ The message was checked by ESET Smart Security. http://www.eset.com
Well, Lets go back to your original code and cut out the decoration: $result = mysql_query("SELECT * FROM counties WHERE name = 'checked'") $county_total=mysql_num_rows($result); while($row = mysql_fetch_array($result)) {
    echo $row['name'];
} echo "$county_total"; That code should do pretty much what you want... But, because you only show the number of records AFTER the loop, you could do: $result = mysql_query("SELECT * FROM counties WHERE name = 'checked'") $county_total = 0; while($row = mysql_fetch_array($result)) {
    echo $row['name'];
    $county_total++;
} echo "$county_total"; -- Peter Ford, Developer phone: 01580 893333 fax: 01580 893399 Justcroft International Ltd. www.justcroft.com Justcroft House, High Street, Staplehurst, Kent TN12 0AH United Kingdom Registered in England and Wales: 2297906 Registered office: Stag Gates House, 63/64 The Avenue, Southampton SO17 1XS

« previous php.general (#311521) next »