|
Posted by Rik on 07/16/06 18:47
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
Navigation:
[Reply to this message]
|