Jump to content

‘while loop’ guidance


stu1040

Recommended Posts

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'] .""; 
}

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.