Jump to content

variable scope: mysql and while loop


masteroleary

Recommended Posts

I am using a while loop to iterate through each row and I also need to [i]echo $row['name'][/i] later in the script but since I am setting the $row variable in the while loop it isnt visible outsite of the loop. How can I iterate through each row and be able to call another value from the array?
[code]$query = "SELECT banner.*, model_profile.model_id FROM banner, model_profile
WHERE banner.id = '".$_POST['banner']."'AND model_profile.banner_id LIKE '%".$_POST['banner']."%'";
$result = mysql_query($query) or die($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$aModels[] = $row['model_id'];
print_r($row); //Prints all values
}
print_r($row); //prints no values at all (outsite of loop)
[/code]
Link to comment
Share on other sites

[code]
while ($r=mysql_fetch_row($q))
{
      $stash[$r['id']]=$r['banner'];
}
[/code]

So now , whenever you want,  just say $stash[12].
Once you consider looping through $stash, then you're looping through the result for a second time, which isn't efficent.

All this being said, your query is using banner.id ,  are you sure you are expecting more than one row?
Link to comment
Share on other sites

I am expecting more than one row. And I am pulling the info needed to be called from the DB, not the form. I agree looping through twice isnt efficient.

The purpose of this code is to play part in a form that can either add a new row to a table or modify an existing row. When I call $row['name'] later it is to fill the html value attribute of the name textfield with the current information. Then the user can edit and hit update. While there will only be one 'name', multiple 'model_id' values are returned and used to preselect models from a select menu after loading a banner. A simple DB management system. So I still have the same question. I tried this hoping to reset the var outsite the loop but to no success.  :'(
[code]
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
$aModels[] = $row['model_id'];
print_r($row); //Prints all values
}
$row = mysql_fetch_array($result, MYSQL_ASSOC);
print_r($row); //still no values at all (outsite of loop)[/code]
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.