homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

PHP Principles Arrays
Back to the real basic, but it has me

 11:33 am on Oct 2, 2003 (gmt 0)

I am having a hard time with what I know is a real basic thing and it is driving me nuts :)

I have a query that should return 1-3 values. I can get it to pring the first one, but no chance with the second. I must be missing something so simple but I cannot fathom it.

$sql = mysql_query("SELECT feild1, feild2 FROM table WHERE id = 1 ");
$link = mysql_fetch_assoc($sql);

some sort of forech here that is driving me nuts {
$anchor = $link["anchor"];
$url = $link["url"];
print "<a href=\"$url\">$anchor</a>\r\n";

did you know it is help a gimp week



 11:40 am on Oct 2, 2003 (gmt 0)

while ($link = mysql_fetch_assoc($sql)) {
echo $link["anchor"];
echo $link["url"];



 11:41 am on Oct 2, 2003 (gmt 0)

Is ID a unique value?



 11:46 am on Oct 2, 2003 (gmt 0)


id is the category, so it is not a unique value


 11:51 am on Oct 2, 2003 (gmt 0)


That worked thanks. But as with all homework questions you have to find out why it does and what the hell I was doing wrong.

Any pointers?


Paul in South Africa

 11:58 am on Oct 2, 2003 (gmt 0)

mysql_fetch_assoc returns an asscociative array corresponding to the fetched row or will return FALSE if there are no more rows to fetch.

while ($link = mysql_fetch_assoc($sql)) will do whatever you tell it to do with each row that is returned and once the end is reached ie mysql_fetch_assoc($sql) returns FALSE the next part of your script will be executed.


 12:09 pm on Oct 2, 2003 (gmt 0)

ukgimp: rewrite the code as this and I think you will understand better what is happening.

$sql = "SELECT feild1, feild2 FROM table WHERE id = 1"; # The statement

$result_set = mysql_query($sql); # get the result set for the statement - can be nothing.

while ( $row = mysql_fetch_assoc($result_set)) { # while it is possible to fetch an assoc_array do so...
$anchor = $row["anchor"];
$url = $row["url"];
print "<a href=\"$url\">$anchor</a>\r\n";



 3:39 pm on Oct 2, 2003 (gmt 0)

$sql = mysql_query("SELECT feild1, feild2 FROM table WHERE id = 1 ");

At this point, $sql will contain the ENTIRE result set, 1 row, or 2 rows, or 3 rows.

$link = mysql_fetch_assoc($sql);

$link will now contain the first row FETCHED. If you had 1 row returned in the result set ($sql), your code would work as you have it. However, if you had 3 rows returned, your FETCH is only going to get you the first row. You need to LOOP through the result set, as the others here have stated.


Your variable, $sql, is a RESULT set which is like a whole spreadsheet of the data retrieved:


whereas $link will contain one row at a time, fetched via mysql_fetch_assoc:


Make sense?

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved