0
<?php
$query = mysql_query("SELECT `uid`,`name` FROM `location`");
while($result = mysql_fetch_assoc($query)) {
    list($location_id, $location_name) = $result;
?>
<option value="contactLocation.php?view_id=<?php echo $location_id; ?>"><?php echo $location_name; ?></option>
<?php
}
?>

Can someone help me and tell me why this isn't working? I'm trying to put the uid and name columns into those two variables in the list.

2
  • 2
    sigh Do you really need to be asked what "isn't working" is supposed to mean? Commented Nov 1, 2013 at 23:21
  • usually I just do $result['uid'] and $result['name'] but i want to put this all in one array to seperate variables. I seen this method used before but not exactly sure if this is right. It looks correct. $result is an array. edit: The variables are empty? Commented Nov 1, 2013 at 23:23

2 Answers 2

1

You’re fetching an associative array. You can use mysql_fetch_row or mysql_fetch_array instead for list() assignment.

Better yet, upgrade to PDO! (Or MySQLi!)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it seems mysql_fetch_assoc doesn't work with list() for php for some reason. Cheers thank you for your time and help.
@TheCryptKeeper: Yep, see the notes section in the docs; it only works with numeric arrays.
0

You can also assign it right from the fetch:

<?php
$query = mysql_query("SELECT `uid`,`name` FROM `location`");
while(list($location_id, $location_name) = mysql_fetch_array($query)) {
?>
   <option value="contactLocation.php?view_id=<?php echo $location_id; ?>"><?php echo  $location_name; ?></option>
<?php
}
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.