Page 1 of 1

mysql_fetching problem

Posted: Wed Nov 09, 2005 2:20 pm
by Kilgore Trout
i’m attempting to make a drop down list in an html form where the select options are pulled from a field (name) in a mysql database table (properties).

Code: Select all

$query = "SELECT name FROM properties";
$result = mysql_query($query, $connection);

// later, inside the form
$i=0;
while($prop = mysql_fetch_array($result)){
	echo '<option value="'.$prop[$i].'">'.$prop[$i].'</option>';
	$i++;
}
at first i was using a foreach loop but thought while would save a line or two. with this code i get a drop down menu showing the first ‘name’ in the field and then blanks for each other ‘name’ in the field. it’s iterating the correct number of times, just not echoing anything. is mysql_fetch_array the wrong function to use or is it something silly like the wrong query?

Re: mysql_fetching problem

Posted: Wed Nov 09, 2005 2:23 pm
by foobar
Try this:

Code: Select all

$query = "SELECT name FROM properties";
$result = mysql_query($query, $connection);

// later, inside the form
$i=0;
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
	echo '<option value="'.$row['name'].'">'.$row['name'].'</option>';
}

Re: mysql_fetching problem

Posted: Wed Nov 09, 2005 2:31 pm
by Luke
foobar wrote:mysql_fetch_array($result, MYSQL_ASSOC)
isn't that the same as mysql_fetch_assoc()

got it

Posted: Wed Nov 09, 2005 2:36 pm
by Kilgore Trout
all i needed was the ['name'] in place of the $i. THANKS!!!

i think mysql_fetch_array defaults at both MYSQL_ASSOC and MYSQL_NUM doesn't it? (works like mysql_fetch_assoc and mysql_fetch_row)