masteroleary Posted January 23, 2007 Share Posted January 23, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/35316-variable-scope-mysql-and-while-loop/ Share on other sites More sharing options...
simcoweb Posted January 23, 2007 Share Posted January 23, 2007 Judging from your $query, you're getting this info from a form? If so, just post the variables ahead of your while loop so you can use them again:$banner = $_POST['banner'];Then you can echo it anywhere. Quote Link to comment https://forums.phpfreaks.com/topic/35316-variable-scope-mysql-and-while-loop/#findComment-166930 Share on other sites More sharing options...
bibby Posted January 23, 2007 Share Posted January 23, 2007 [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? Quote Link to comment https://forums.phpfreaks.com/topic/35316-variable-scope-mysql-and-while-loop/#findComment-166933 Share on other sites More sharing options...
masteroleary Posted January 23, 2007 Author Share Posted January 23, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/35316-variable-scope-mysql-and-while-loop/#findComment-166947 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.