Looping Thru Database Query

windows

Guest
The following is a modified snippet of code (copied from a book) i am using to loop thru results of a db query and display them on screen (where 'link' is a column name in the db):<br /><br />echo "<table>";<br />while ($Array = mysql_fetch_array($result)) { <br />$link = $Array['link'];<br />echo "<tr><td>$link</td></tr>";<br />}<br />echo "</table>";<br /><br />The problem i am having is that the table always contains one less row than the db query.<br /><br />To test i have added the following before the "while loop" begins:<br />$rows = mysql_num_rows($result);<br />echo "$rows";<br /><br />Sure enough if my table has 4 rows, the $rows echo shows 5 (if the $rows shows 28, the actual number or rows in the table created is only 27).<br /><br />Can any one please comment on my error and put me on the right track.<br /><br />Thank you.<!--content-->
The only thing I can figure is that your code is doing something between the execution of the query and your while loop that is advancing the query result's internal row pointer 1 record, causing it to not be displayed by the while loop.<br /><br />You could try adding the following code (which resets the internal row pointer) just before your while statement and see if it displays all the records:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->mysql_data_seek($result, 0);<!--c2--></div><!--ec2--><!--content-->
Thanks for the reply and suggestion.<br /><br />I tried adding the above code and it didn't work.<br /><br />Before posting originally i put the above code into its own file (with nothing else other than the db query) and i still come up one row short, which is why i'm so stumped, as i think i've excluded any other parts of the program by 'testing' it this way.<br /><br />Any other ways to go about the above (such as setting up a counter with the number of rows and then looping while the counter is =< the number of rows?) This seems like it would be another way to accomplish the same thing, but i'm not sure how to set up the array and use them in the code. If this is another way to go about the same thing, can you explain how i could set up the array to use format such as:<br /><br />$link["$i"]<br /><br />where "i" would be a counter and also act as the 'key' for the $link array (hope i got my terminology correct, but if not, hopefully you understand what i'm trying to do)<br /><br />Thanks.<!--content-->
Using a counter as a key for your $link array wouldn't work the way you think it does. $link is an array, but is an array that holds only one row of data at a time. The array key would specify what column (field) to retrieve, not what row.<br /><br />One other thing I thought of as a possibility: one of the result rows has a null 'link' field. (I'm thinking either the first or last row).<br /><br />Try this code and see what it displays:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->echo "<table>";<br />$i = 0;<br />while ($Array = mysql_fetch_array($result)) {<br />$i++;<br />$link = $Array['link'];<br />echo "<tr><td>$i</td><td>$link</td></tr>";<br />}<br />echo "</table>";<!--c2--></div><!--ec2--><!--content-->
Thanks for the reply(s).<br /><br />Actually got it solved (with some TCH help), had this line which was 'using' the first row of data:<br /><br />$row = mysql_fetch_array($result, MYSQL_ASSOC);<br /><br />I thought all the above did was use the column heading as variables, but apparently i was wrong, and it was 'using' the first row of data, thus starting the loop at row 2.<br /><br />Hope my posting/follow-up can help somebody else down the road.<br /><br />Thanks again for the assistance.<!--content-->
Along the same loop lines....<br /><br />Some fields in my db are empty, and i need to loop thru a db querry and retrieve the first non-empty value from columnX (so it can be used later in the program), at which point the loop can stop.<br /><br />I tried:<br />while ($x == "") {<br />while ($newArray = mysql_fetch_array($result)) { <br />$x = $newArray['x'];<br />}<br />}<br /><br />My thought process is that "while $x is empty, the loop will continue. Once $x has a value (which would be a non-integer number from the db) the above loop would stop", but that's not the case and the above continues to loop thru all the results (and maybe more as it sometimes 'times out' at over 30 seconds).<br /><br />Can you get me back on track and steer me in the right direction?<br /><br />Thank you.<!--content-->
I guess I would try something like the following:<br /><!--c1--><div class='codetop'>CODE</div><div class='codemain'><!--ec1-->while ($newArray = mysql_fetch_array($result)) {<br />    if ($x = $newArray['x']) { break; }<br />}<!--c2--></div><!--ec2--><!--content-->
Thanks... i've used 'break' in my codes before, but just couldn't think of it this time around and couldn't figure out to exit a function without exiting the entire code.<br /><br />I'm all set for now.<br /><br />Thank you again for your help.<!--content-->
 
Back
Top