Solved

adding items from database into an array

Posted on 2007-11-20
5
222 Views
Last Modified: 2008-02-01
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?


0
Comment
Question by:markmchugh
  • 2
  • 2
5 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 20320756
$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];
                                    }
     
                              }
0
 
LVL 82

Expert Comment

by:hielo
ID: 20320799
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"]);
}
                              }

0
 
LVL 4

Expert Comment

by:simonkin
ID: 20321506
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

0
 

Author Comment

by:markmchugh
ID: 20321726
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];
                                    }
      
0
 
LVL 4

Accepted Solution

by:
simonkin earned 500 total points
ID: 20321775
Ok, 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();
 

                // Add the -- to the start of the array

                $options[] = "--";

        

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

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now