dlcmpls Posted March 14, 2007 Share Posted March 14, 2007 Hello everyone. I have what I think is a simple issue, but it's turned my mind to mush! Here's the situation. I have a query that pulls 4 records from a mysql db. Let's say that each record is the name of a fruit: Orange, Lemon, Lime, Grape. The fruits are in that order in the db. Record 1 is "Orange", Record 2 is "Lemon" etc. My table has 2 fields - FruitID and FruitName, and 4 records. I have the query working with a "while" statement so the list of fruit prints to the screen just fine. Of course, the list prints as reflected in the db. Orange is first, followed by Lemon, then Lime etc. What I'd like to do is reorder the fruit for printing. Or, to put it another way, what if I wanted to print each Fruit name just once, but in different places in a web page, not as a list? How do I do this? I assume I have to use some sort of array and assign each fruit to a variable that can be echoed to the screen. Here's my working code for reference: $Link = mysql_connect("$Host", "$User", "$Password") or die ('I cannot connect to the database.' . mysql_error()); mysql_select_db("$DBName"); $fruitquery = mysql_query("SELECT * FROM fruit"); while($Row = mysql_fetch_array($fruitquery)) { print ("$Row[FruitName]"); } Any suggestions? I know this can't be difficult but my brain just isn't grasping the answer. Thanks dlc Quote Link to comment https://forums.phpfreaks.com/topic/42716-assign-results-of-query-to-user-defined-variables/ Share on other sites More sharing options...
per1os Posted March 14, 2007 Share Posted March 14, 2007 <?php $Link = mysql_connect("$Host", "$User", "$Password") or die ('I cannot connect to the database.' . mysql_error()); mysql_select_db("$DBName"); $fruitquery = mysql_query("SELECT * FROM fruit"); while($Row = mysql_fetch_array($fruitquery)) { $fruitNames[$Row['fruitid']] = $Row['FruitName']; } print $fruitNames[0]; // should print Orange sort($fruitNames); print $fruitNames[0]; // should print Grape ?> That is how you get it in an array and you can sort the array to display particular values. --FrosT Quote Link to comment https://forums.phpfreaks.com/topic/42716-assign-results-of-query-to-user-defined-variables/#findComment-207415 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.