stu1040 Posted January 9, 2012 Share Posted January 9, 2012 Come on Guys and Girls! I’ve worked out the ‘while loop’ for numbers, it’s quite easy (when you know how). Being 42 (in 2 weeks) my brain is a little slow, and getting slower, (age). You can tell i’ve been working hard because i’m using a lot of brackets. Where am I going wrong? The code below comes from 5 distinct links of cities, birmingham, glasgow, london ... When I click the link such as ‘birmingham’ I arrive at a page where everything related to ‘birmingham’ should be displayed, and so to for the other cities. However only one record is being displayed when I know there are 3 records of each city. I’ve figured the ‘loop’ for numbers, but to get pacific related data i’m a little stumped. Guidance to any tutorials or direction appreciated, but no shouting since i’m simply learning. Thanks, Stuart. Laid out this way for easy reading. $id = mysql_real_escape_string(trim($_GET['id'])); $data = mysql_query("SELECT location FROM cities WHERE id = '$id' ") or die(mysql_error()); $info = mysql_fetch_array( $data ); While ??? { Print "".$info['location'] .""; } Quote Link to comment https://forums.phpfreaks.com/topic/254681-%E2%80%98while-loop%E2%80%99-guidance/ Share on other sites More sharing options...
premiso Posted January 9, 2012 Share Posted January 9, 2012 while ($info = mysql_fetch_array( $data )) { echo $info['location']; } Is that what you are looking for? Quote Link to comment https://forums.phpfreaks.com/topic/254681-%E2%80%98while-loop%E2%80%99-guidance/#findComment-1305943 Share on other sites More sharing options...
Psycho Posted January 9, 2012 Share Posted January 9, 2012 Well, I have one additional year on you so don't try using your age as an excuse. I have no idea what you mean by I’ve figured the ‘loop’ for numbers, but to get pacific [sic] related data i’m a little stumped. A while loop has nothing to do about numbers. A while loop continues as long as the condition is true. So, when getting a result from a database query you simply need to create a condition to get the next record in the result set (typically using one of the mysql_fetch_ functions). Then the loop will get a new record on each iteration of the while() loop and will exit the loop once there are no more records to retrieve. So, you just need to move the $info = mysql_fetch_array( $data ); to be your condition for the while loop. Also, don't use mysql_real_escape_string() for variables that are supposed to be numeric. It won't prevent errors from non-numeric input. $id = intval($_GET['id']); $query = "SELECT location FROM cities WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error()); while($row = mysql_fetch_assoc($result)) { echo "{$info['location']}<br>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/254681-%E2%80%98while-loop%E2%80%99-guidance/#findComment-1305944 Share on other sites More sharing options...
stu1040 Posted January 9, 2012 Author Share Posted January 9, 2012 Thanks, I’ve actually tried - While($row = mysql_fetch_assoc($result)) - The result is no data, and the address bar shows something is getting through but not displaying - locations.php?id=5' - These links go to the same page with the relevant data, such as london, birmingham. This is where the city links come from $data = mysql_query("SELECT id, location FROM cities GROUP BY location ORDER BY id ASC") OR DIE (mysql_error()); while($info = mysql_fetch_array($data)) Print "<a href=locations.php?id=".$info['id']."'>".$info['location'] ."</a><br />"; Loop for numbers, all the tutorials I find seem to use numbers as an example. And thanks for the update on mysql_real_escape_string Quote Link to comment https://forums.phpfreaks.com/topic/254681-%E2%80%98while-loop%E2%80%99-guidance/#findComment-1305945 Share on other sites More sharing options...
litebearer Posted January 9, 2012 Share Posted January 9, 2012 Had to interject here; 66 this friday - Love seeing the how age is all a matter of perspective. Quote Link to comment https://forums.phpfreaks.com/topic/254681-%E2%80%98while-loop%E2%80%99-guidance/#findComment-1305956 Share on other sites More sharing options...
Psycho Posted January 10, 2012 Share Posted January 10, 2012 I just noticed that in my sample code I used $row in the while loop but didn't change the variable in the while loop. They need to be the same. Anyway, if you are still not getting any data after fixing that, then the query is not getting any results. Add some debugging info to validate what is actually happening. Also, do a better job of structuring your code. That last bit you posted is - sorry to say - awful. $id = intval($_GET['id']); $query = "SELECT location FROM cities WHERE id = '$id'"; $result = mysql_query($query) or die(mysql_error()); //Debugging info echo "The GET value for id is {$_GET['id']}<br>\n"; echo "The value for the id variable is {$id}<br>\n"; echo "The query is: {$query}<br>\n"; echo "The query generated " . mysql_num_rows($result) . " results<br>\n"; while($row = mysql_fetch_assoc($result)) { echo "{$row['location']}<br>\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/254681-%E2%80%98while-loop%E2%80%99-guidance/#findComment-1305972 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.