Retrieving records within a PHP function
The following code is taken from a perfect working drop down list, and then when I put it into a function it breaks it! Am I doing something wrong here?
<?php
require "conne开发者_C百科ct.php";
//create country lists
function records() {
$countryOptions = '';
$query = "SELECT DISTINCT country FROM regions";
$result = mysql_query($query);
if (!$result) {
$countryOptions = "<option>Error Retrieving Records</option>\n";;
}
else {
while ($row=mysql_fetch_assoc($result)) {
$countryOptions .= "<option value=\"{$row['country']}\">";
$countryOptions .= "{$row['country']}";
$countryOptions .= "</option>\n";
}
}
}
echo records();
?>
You're not outputting $countryOptions
anywhere.
Either add
echo $countryOptions;
at the end of the function or better yet use
return $countryOptions;
and call the function like this:
echo records();
(or implement it to fit your exact needs - it's hard to tell how you use it in your own code)
精彩评论