我有两个从数据库中检索值的函数。当我访问函数内部的连接 ( $connection
) 时,我无法访问数据库。我正在使用 mysql+PHP
这是代码:
<?php
function getTierOne()
{
require_once('../config.php');
$provincequery="SELECT provinces.ProvinceID, provinces.ProvinceName FROM provinces WHERE ProvinceID > 0";
$result = $connection->query($provincequery);
while($province = $result->fetch_assoc())
{
echo '<option value="'.$province['ProvinceID'].'">'.$province['ProvinceName'].'</option>';
}
}
//**************************************
// First selection results //
//**************************************
if(isset($_GET['func']) && $_GET['func'] == "province")
{
province($_GET['drop_var']);
}
function province($drop_var)
{
require_once('../config.php');
$district_query = "SELECT districts.DistrictID, districts.DistrictName, provinces.ProvinceName FROM districts, provinces WHERE provinces.ProvinceID=districts.ProvinceID AND districts.DistrictID > 0 AND provinces.ProvinceID='$drop_var'";
$district_query_run= $connection->query($district_query);
echo '<select name="district" id="district" style="width:150px;">
<option value="0" disabled="disabled" selected="selected">Select Your District</option>';
while($district= $district_query_run->fetch_assoc())
{
echo '<option value="'.$district['DistrictID'].'">'.$district['DistrictName'].'</option>';
}
echo '</select>';
echo "<script type=\"text/javascript\">
$('#wait_2').hide();
$('#district').change(function(){
$('#wait_2').show();
$('#result_2').hide();
$.get(\"ProDisPds/func.php\", {
func: \"district\",
drop_var: $('#district').val()
}, function(response){
$('#result_2').fadeOut();
setTimeout(\"finishAjax_tier_three('result_2', '\"+escape(response)+\"')\", 400);
});
return false;
});
</script>";
}
?>