Forum Moderators: coopster
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
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.
$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";
}
HTH
$sql = mysql_query("SELECT feild1, feild2 FROM table WHERE id = 1 ");
$link = mysql_fetch_assoc($sql);
Compare:
Your variable, $sql, is a RESULT set which is like a whole spreadsheet of the data retrieved:
anchor1,url1
anchor2,url2
anchor3,url3
whereas $link will contain one row at a time, fetched via mysql_fetch_assoc:
anchor1,url1
Make sense?