Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SQL中使用的PHP变量_Php_Sql - Fatal编程技术网

SQL中使用的PHP变量

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 = '".$

我的代码检查是否有$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 = '".$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
        }


    }
?>

是否按一年中的当前月份进行查询是的,因为内爆连接数组。因此,如果您有一个月,您还必须将其存储在一个数组中。