Welcome to WebmasterWorld Guest from 54.158.54.179

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

PHP Principles Arrays

Back to the real basic, but it has me

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

WebmasterWorld Senior Member 10+ Year Member



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)

10+ Year Member



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

Cheers

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

WebmasterWorld Senior Member nick_w is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Is ID a unique value?

Nick

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

WebmasterWorld Senior Member 10+ Year Member



Nick

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

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

WebmasterWorld Senior Member 10+ Year Member



mogwai

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?

Cheers

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

10+ Year Member



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)

WebmasterWorld Senior Member 10+ Year Member



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";
}

HTH

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

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



$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.

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?

 

Featured Threads

Hot Threads This Week

Hot Threads This Month