简体   繁体   中英

using List() with array PHP, Why won't this work?

<?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.

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!)

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
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM