MySQL 数据显示 php。 If select show selected data, If no select;显示整个数据。我怎样才能做到这一点?
MySQL Data show by php. If select show selected data, If no select; show whole data. How can I do this?
这是一个献血网站。如果有人想看他所在地区的捐赠者;他select blood-grp, district and area.但是如果他不select区字段,他什么也看不到。但我希望他看到整个地区的捐助者。我怎样才能做到这一点?这是我的代码
$blood-grp=$_POST['blood-grp'];
$district=$_POST['district'];
$area=$_POST['area'];
$sql="SELECT * FROM $tbl_name WHERE blood-grp='$blood-grp' && district='$district' && area='$area'";
使用if
检查是否已设置。旁注:连字符 (-
) 不允许出现在变量名
中
$blood_grp=$_POST['blood-grp'];
$area=$_POST['area'];
if (isset($_POST['district'])) {
$district=$_POST['district'];
$sql = "SELECT * FROM $tbl_name WHERE `blood-grp`='$blood_grp' AND district='$district' AND area='$area'";
} else {
$sql = "SELECT * FROM $tbl_name WHERE `blood-grp`='$blood_grp' AND area='$area'";
}
试试这个你就会确定
如果他不select他所在的地区那么如果用户已经注册然后带用户去经纬度
if($_POST['area']=="")
{
$sql="SELECT * FROM $tbl_name WHERE blood-grp='$blood-grp' AND area='$userarea'";
} else {
$sql="SELECT * FROM $tbl_name WHERE blood-grp='$blood-grp' AND district='$district' AND area='$area'";
}
您必须使用连接和 if 来更有条理地写下您的查询。
这样只有用户选择的参数才会在您的查询中实际实现。
变量名中不允许使用“-”字符,我已将其更正为“_”。
$blood_grp=$_POST['blood_grp'];
$district=$_POST['district'];
$area=$_POST['area'];
$sql="SELECT *
FROM $tbl_name
WHERE ";
if(strlen($blood-grp) > 0) {
$query .= "blood_grp = '$blood_grp' AND ";
}
if(strlen($district) > 0) {
$query .= "district = '$district' AND ";
}
if(strlen($area) > 0) {
$query .= "area = '$area' AND ";
}
//Remove final " AND " by cutting off last 5 characters to make the query valid:
$query = substr($query, 0, strlen($query) - 5);
echo $query;
这是一个献血网站。如果有人想看他所在地区的捐赠者;他select blood-grp, district and area.但是如果他不select区字段,他什么也看不到。但我希望他看到整个地区的捐助者。我怎样才能做到这一点?这是我的代码
$blood-grp=$_POST['blood-grp'];
$district=$_POST['district'];
$area=$_POST['area'];
$sql="SELECT * FROM $tbl_name WHERE blood-grp='$blood-grp' && district='$district' && area='$area'";
使用if
检查是否已设置。旁注:连字符 (-
) 不允许出现在变量名
$blood_grp=$_POST['blood-grp'];
$area=$_POST['area'];
if (isset($_POST['district'])) {
$district=$_POST['district'];
$sql = "SELECT * FROM $tbl_name WHERE `blood-grp`='$blood_grp' AND district='$district' AND area='$area'";
} else {
$sql = "SELECT * FROM $tbl_name WHERE `blood-grp`='$blood_grp' AND area='$area'";
}
试试这个你就会确定
如果他不select他所在的地区那么如果用户已经注册然后带用户去经纬度
if($_POST['area']=="")
{
$sql="SELECT * FROM $tbl_name WHERE blood-grp='$blood-grp' AND area='$userarea'";
} else {
$sql="SELECT * FROM $tbl_name WHERE blood-grp='$blood-grp' AND district='$district' AND area='$area'";
}
您必须使用连接和 if 来更有条理地写下您的查询。 这样只有用户选择的参数才会在您的查询中实际实现。
变量名中不允许使用“-”字符,我已将其更正为“_”。
$blood_grp=$_POST['blood_grp'];
$district=$_POST['district'];
$area=$_POST['area'];
$sql="SELECT *
FROM $tbl_name
WHERE ";
if(strlen($blood-grp) > 0) {
$query .= "blood_grp = '$blood_grp' AND ";
}
if(strlen($district) > 0) {
$query .= "district = '$district' AND ";
}
if(strlen($area) > 0) {
$query .= "area = '$area' AND ";
}
//Remove final " AND " by cutting off last 5 characters to make the query valid:
$query = substr($query, 0, strlen($query) - 5);
echo $query;