Re: Only showing 20 lines per page...



Mike wrote:
OK, think I have got it...

I can do the count using select count(id) and the maths to work out
the start and finish.

This is how I think using mysql_fetch_array works...

$conn = //db connection stuff
$sql = "select VALUES from TABLE LIMIT X,X";
$sql1 = "mysql_query($sql,$conn) or die(mysql_error());

while ($results = mysql_fetch_array($sql1)) {
$value1 = $results['value1'];
$value2 = $results['value2'];
.
.
.etc..
//create a display block
echo $value1
echo $value2
.
.
.etc..
}

More or less, but why assigning intermediate variables?
Lets for instance say I have a db with an image url & an alt-text

while ($results = mysql_fetch_array($sql1)) {
echo "<img src=\"{$results['url']}\" alt=\"{$results['alt']}\" />";
}

I think I was confused as I'm used to seeing a counter var incremented
as the check to see when to dump out of the while loop. I couldn't
see how it knew to move to the next row each time. Can you explain?

From the manual:
Returns an array that corresponds to the fetched row and moves the internal
data pointer ahead.

So, by setting the pointer forward, the next row will be returned the next
time the function is called. Because it returns false if there are no more
rows it the while loop will stop after the last row.

Grtz,
--
Rik Wasmus


.



Relevant Pages

  • Re: Only showing 20 lines per page...
    ... I can do the count using select countand the maths to work out the ... echo $value1 ... as the check to see when to dump out of the while loop. ...
    (alt.php)
  • Re: Reading a variable line by line with while loop
    ... done < `echo "$Variable"` ... echo "History - $History" ... The last echo command returns nothing, but if I put an echo command in ... the loop either before, or after the replace spaces command, it echoes ...
    (Ubuntu)
  • Re: 2.6.16-rc4: known regressions
    ... either with a command line argument, or with just the early bootup initrd ... Is there a way to tell the kernel about which is the root device other ... a loop with one second delay. ... echo -n "Waiting for root device to appear" ...
    (Linux-Kernel)
  • Re: [PHP] 2 errors I can not understand
    ... echo is not a function. ... multiple args is probably insignificant, ... and super-long loop... ... function isprime ...
    (php.general)
  • Re: Startup Script but wait for SQL to start
    ... In each case there is supposed to be a Username in the log file. ... @echo off ... if Errorlevel 1 goto Loop ... And it sucessfully starts up the Indexer. ...
    (microsoft.public.windows.server.general)