Ryflex Posted December 16, 2010 Share Posted December 16, 2010 Hi all, This is a piece of code of my php page and what I need is that $n1 till $n4 is filled. Somehow only $n1 is filled. Can anyone tell me what I'm doing wrong here. Thnx Ryflex $sql = "SELECT name FROM unittype WHERE type = 1"; $result = mysql_fetch_array(mysql_query($sql)) or die("Could not find units"); $n1 = $result[0]; $n2 = $result[1]; $n3 = $result[2]; $n4 = $result[3]; echo "$n1 <BR> $n2 <BR> $n3 <BR> $n4"; Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 16, 2010 Share Posted December 16, 2010 mysql_fetch_array() only fetches ONE ROW from the result set. You would need to use a while(){} loop to fetch all the rows from the result set. Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148191 Share on other sites More sharing options...
Ryflex Posted December 16, 2010 Author Share Posted December 16, 2010 Hi PFMaBiSmAd, If a mysql_fetch_array query only fetches one row what would a while loop do then??? Thnx Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148222 Share on other sites More sharing options...
harristweed Posted December 16, 2010 Share Posted December 16, 2010 $sql= "SELECT name FROM unittype WHERE type = 1"; $result=mysql_query($sql) or die("Could not find units"); while($data=mysql_fetch_array($result)){ echo"$data[0]<br />\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148230 Share on other sites More sharing options...
Ryflex Posted December 16, 2010 Author Share Posted December 16, 2010 Hi harristweed, but my problem is not getting the information echoed but I need them in the variables $n1 till $n4 Thnx Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148232 Share on other sites More sharing options...
Pikachu2000 Posted December 16, 2010 Share Posted December 16, 2010 To clarify what was being said, mysql_fetch_array() only retrieves one row at a time. Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148235 Share on other sites More sharing options...
Ryflex Posted December 16, 2010 Author Share Posted December 16, 2010 Hi Pikachu2000, I do understand that but is it possible in some other way to get the information in the way I mentioned??? Thnx Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148236 Share on other sites More sharing options...
PFMaBiSmAd Posted December 16, 2010 Share Posted December 16, 2010 A) Don't use a series of named variables, $n1, $n2, ... Use an array (arrays are for sets of data, which is what you have.) B) Instead of an echo statement, you would assign the value to an array element. Programming is about doing what you want when and where you want it to happen. C) Done! Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148237 Share on other sites More sharing options...
Ryflex Posted December 16, 2010 Author Share Posted December 16, 2010 Hi all, I got this far and got what is underneath but then how do I get the right values??? Thnx Ryflex $sql = "SELECT name FROM unittype WHERE type = 1"; $result = mysql_query($sql) or die("Could not find units"); while($data = mysql_fetch_assoc($result)) { $n[] = $data; } print_r($n); echo $n[0],$n[1],$n[2],$n[3]; Array ( [0] => Array ( [name] => gunner ) [1] => Array ( [name] => marine ) [2] => Array ( [name] => sniper ) [3] => Array ( [name] => special ops ) ) ArrayArrayArrayArray Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148246 Share on other sites More sharing options...
Ryflex Posted December 16, 2010 Author Share Posted December 16, 2010 Hi all, I found out how to get to my info but it's kind of awkward... I have to use $n[1][name] to get my info out. Is there a way to get around the [name] part and just make it one-dimensional??? Thnx Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148252 Share on other sites More sharing options...
kenrbnsn Posted December 16, 2010 Share Posted December 16, 2010 Don't store the full array, only store the information you need: <?php $sql = "SELECT name FROM unittype WHERE type = 1"; $result = mysql_query($sql) or die("Could not find units"); while($data = mysql_fetch_assoc($result)) { $n[] = $data['name']; } print_r($n); echo implode(',',$n); ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148255 Share on other sites More sharing options...
Ryflex Posted December 16, 2010 Author Share Posted December 16, 2010 Hi kenrbnsn, Thnx this really helped me. Not only kenrbnsn thnx but all who responded thank you. Ryflex Quote Link to comment https://forums.phpfreaks.com/topic/221887-array-out-of-a-database/#findComment-1148259 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.