mysql_fetching problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Kilgore Trout
Forum Newbie
Posts: 10
Joined: Sun Oct 30, 2005 3:40 pm
Location: MI

mysql_fetching problem

Post 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?
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Re: mysql_fetching problem

Post 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>';
}
User avatar
Luke
The Ninja Space Mod
Posts: 6424
Joined: Fri Aug 05, 2005 1:53 pm
Location: Paradise, CA

Re: mysql_fetching problem

Post by Luke »

foobar wrote:mysql_fetch_array($result, MYSQL_ASSOC)
isn't that the same as mysql_fetch_assoc()
User avatar
Kilgore Trout
Forum Newbie
Posts: 10
Joined: Sun Oct 30, 2005 3:40 pm
Location: MI

got it

Post 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)
Post Reply