Jump to content

Assign results of query to user defined variables


dlcmpls

Recommended Posts

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

 

 

 

 

<?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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.