SQL中使用的PHP变量
我的代码检查是否有$GET值,如果没有,则分配数组的所有值。 看起来很简单,不知道为什么它不起作用SQL中使用的PHP变量,php,sql,Php,Sql,我的代码检查是否有$GET值,如果没有,则分配数组的所有值。 看起来很简单,不知道为什么它不起作用 if(isset($_GET["smonth"])) {$smonth= $_GET["smonth"]; } else {$smonth =12;} working , but not what I want else {$smonth =array (1,2,3,4,5,6,7,8,9,10,11) ;} 之后,我想在SQL中使用它: and d.month_of_year = '".$
if(isset($_GET["smonth"])) {$smonth= $_GET["smonth"];
}
else {$smonth =12;} working , but not what I want
else {$smonth =array (1,2,3,4,5,6,7,8,9,10,11) ;}
之后,我想在SQL中使用它:
and d.month_of_year = '".$smonth."%'
那大概是
and month_of_year = (all values of array) or 1 value)
我的问题:
如果活动月份可用,检查的最佳解决方案是什么?如果没有,请指定所有月份进行查询。谢谢in_array和infrade的内置PHP函数应该可以解决您的问题:
in_array('1', $_GET["smonth"]); // checks if January is in $_GET["smonth"]
implode("," , $_GET["smonth"]); // Pull all of the values out of $_GET["smonth"] as a A STRING
请尝试在(“.intlode(',',$smonth)。”)中的语句
和d.month\u of年“
=
运算符检查单个值。如果要检查多个值,请使用中的
and d.month_of_year in (".$smonth.")
这里还有一个%
,它可以处理之类的查询。
<?php
if(isset($_GET['month'])){
$month = date('m'); //This would give you the index of the current month.
$array = array('01','02','02');
$query = "select * from table where month = ";
if(in_array($month,$array)){
$query = "select * from table where month = '".$month."'";
//Then query here
}
else
{
$query = "select * from table";
$where = "";
foreach($month as $m){
$where .= ' month = "'.$m.'" and ';
}
//There would be a ending and pls just try remove it
$query .= $where;
// then query here
}
}
?>
是否按一年中的当前月份进行查询是的,因为内爆连接数组。因此,如果您有一个月,您还必须将其存储在一个数组中。