Link to home
Start Free TrialLog in
Avatar of markmchugh
markmchugh

asked on

adding items from database into an array

hi, i am using this code to select from a database,


                              $query = "Select * from countys;";
                              $result1 = mysql_query($query);
                        
                              if (mysql_num_rows($result1) > 0)            
                              {
                                    while ($myrow1 = mysql_fetch_array($result1))
                                    {
                                    }
      
                              }


i want to put each item into an array, like this,

$option = array('--','County1','County2') etc etc...

how do i build the array option in the database code above?


Avatar of hielo
hielo
Flag of Wallis and Futuna image

$query = "Select county from countys;";
                              $result1 = mysql_query($query);
                        $option=array();
                              if (mysql_num_rows($result1) > 0)            
                              {
                                    while ($myrow1 = mysql_fetch_array($result1))
                                    {
$option[]=$myrow1[0];
                                    }
     
                              }
IF your query is returning more than one field, as in:
$query = "Select city, county from countys;";
                              $result1 = mysql_query($query);
                        $option=array();
                              if (mysql_num_rows($result1) > 0)            
                              {
                                    while ($myrow1 = mysql_fetch_assoc($result1))
                                    {
$temp=array("city" => $myrow1["city"], "county" =>$myrow1["county"] );
$option[]=$temp;
                                    }
     
You would then retrieve the values as follows:
for($i=0;$i<count($option);++$i)
{
echo( $option[$i]["city"] . " " . $options[$i]["county"]);
}
                              }

Avatar of simonkin
simonkin

Hi,

Try this...


<?php
 
	// Perform the query
	$query = "Select * from countys;";
	$result = mysql_query($query);
                        
	// If there are results
	if (mysql_num_rows($result) > 0) {
	
		// Define the options array
		$options = array();
	
		// Loop through the resultset
		while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
		
			// Write the options array
			$options[] = $row["county"]; // Assuming county is the name of the field you require...
		
		}
		
		// Check the output...
		print_r($options);
	
	}
 
?>

Open in new window

Avatar of markmchugh

ASKER

i want to put '--' a first position in the array, i'm using this for a select box,

this code does not do it

$option[] = '--';
                                    while ($myrow1 = mysql_fetch_array($result1))
                                    {
                                    $option[]=$myrow1[1];
                                    }
      
ASKER CERTIFIED SOLUTION
Avatar of simonkin
simonkin

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial