/    Sign up×
Community /Pin to ProfileBookmark

Problem populating list

Hi,

I want to highlight the appropriate option as selected, when populating a drop down with a query, I’m not sure how refer to the different fields. Can anybody help?

Thankyou

[CODE]

<select name=”ethnicorigin”>
<?php
$link_id = db_connect();
$query = “SELECT * FROM tblethnicorigin;”;
$result = mysql_query($query);
if(!$result) error_message(sql_error());
while ($myrow = mysql_fetch_array($result)) {
echo ” <option value='”. $myrow[ethnicID] .”‘”;
if ($counter == $myrow[ethnicID]){echo ” selected “;}
echo “>” . $myrow[“ethnicOrigin”] . “</option>”;
}
?>
</select> [/CODE]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@purefanJan 15.2006 — what problem are you getting?
Copy linkTweet thisAlerts:
@chazzyJan 15.2006 — Try $myRow['fieldname']
Copy linkTweet thisAlerts:
@HuevoosJan 17.2006 — mmm... I don't quite understand your code

but you should try
[code=php]while ($myrow = mysql_fetch_assoc($result)) {
[/code]
Copy linkTweet thisAlerts:
@chazzyJan 17.2006 — mmm... I don't quite understand your code

but you should try
[code=php]while ($myrow = mysql_fetch_assoc($result)) {
[/code]
[/QUOTE]


There's no reason why he would need to make that switch, btw, as mysql_fetch_array by default returns data in both MYSQL_NUM and MYSQL_ASSOC formats (and incidentally, if you did a foreach on mysql_fetch_array as it is done here, you'll get all of your data twice)
Copy linkTweet thisAlerts:
@NogDogJan 17.2006 — mysql_fetch_assoc() will probably run a millisecond faster and take up a couple bytes less memory than mysql_fetch_array(), but functionally it should make no difference. (It would be marginally more efficient because it only has to create and store one set of array keys instead of two.)
Copy linkTweet thisAlerts:
@chazzyJan 17.2006 — yep NogDog's right, to an extent, and from the php manual


Notes

Performance: An important thing to note is that using mysql_fetch_array() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value.

Note: Field names returned by this function are case-sensitive.

Note: This function sets NULL fields to PHP NULL value.

[/quote]


and

Notes

Performance: An important thing to note is that using mysql_fetch_assoc() is not significantly slower than using mysql_fetch_row(), while it provides a significant added value.

Note: Field names returned by this function are case-sensitive.

Note: This function sets NULL fields to PHP NULL value.

[/quote]
×

Success!

Help @mattastic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...