有没有办法用变量表示表的列名?
如下例所示,在Switch - case:DATE中,我想使用变量$ d_status而不是写入列(visit_date)。 在此先感谢。
$s_id = mysql_prep($s_id);
$d_status = "visit_date";
$searchby = mysql_prep($searchby);
$searchtext = mysql_prep($searchtext);
$query = "SELECT * ";
$query .= "FROM doctor ";
$query .= "WHERE (s_id = $s_id ) AND (d_status = '{$d_status}') ";
switch ($searchby){
case 'd_name':
$query .= "AND ( d_name = '{$searchtext}' )";
break;
case 'd_id':
$query .= "AND ( d_id = $searchtext )";
break;
case 'DATE':
$query .= "AND ( $d_status = '{$searchtext}' )";
break;
Default: null;
break;
$result = mysqli_query($connection, $query);
confirm_query($result);
return $result;
}
答案 0 :(得分:0)
在PHP中允许使用变量作为案例值(不经常使用但允许)(在其他语言中不像c ++那样)
$myTets = 'DATE'
switch ($searchby){
....
case $myTest:
echo 'Test OK';
break;
.....
或者如果你需要$ d_status中包含的dinamic列名,你可以使用string concat
$query .= "AND ( " . $d_status ." = '{$searchtext}' )";