homepage Welcome to WebmasterWorld Guest from 54.211.73.232
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
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
ukgimp

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1822 posted 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

 

mogwai

10+ Year Member



 
Msg#: 1822 posted 11:40 am on Oct 2, 2003 (gmt 0)

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

Cheers

Nick_W

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



 
Msg#: 1822 posted 11:41 am on Oct 2, 2003 (gmt 0)

Is ID a unique value?

Nick

ukgimp

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1822 posted 11:46 am on Oct 2, 2003 (gmt 0)

Nick

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

ukgimp

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1822 posted 11:51 am on Oct 2, 2003 (gmt 0)

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

Paul in South Africa

10+ Year Member



 
Msg#: 1822 posted 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.

gethan

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 1822 posted 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";
}

HTH

coopster

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



 
Msg#: 1822 posted 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.

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?

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