Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/62.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
动态创建WHERE子句PHP/MySQL_Php_Mysql - Fatal编程技术网

动态创建WHERE子句PHP/MySQL

动态创建WHERE子句PHP/MySQL,php,mysql,Php,Mysql,我正在动态生成MySQL查询的WHERE部分。到目前为止,我下面的代码工作得非常好 请注意:\u GET字符串在我的代码中的任何地方都经过验证,但为了将此代码的长度保持在这个问题的合理长度,我将它们直接放在下面。对于任何希望做类似于我正在做的事情并使用我的代码作为基础的人,请确保验证您的字符串以避免mysql注入 /* Loop through each column in the table */ for ( $i=0 ; $i<count($aColumns) ;

我正在动态生成MySQL查询的WHERE部分。到目前为止,我下面的代码工作得非常好

请注意:\u GET字符串在我的代码中的任何地方都经过验证,但为了将此代码的长度保持在这个问题的合理长度,我将它们直接放在下面。对于任何希望做类似于我正在做的事情并使用我的代码作为基础的人,请确保验证您的字符串以避免mysql注入

  /* Loop through each column in the table */

        for ( $i=0 ; $i<count($aColumns) ; $i++ )
        {
             /* check if the column has been marked as searchable and that the param sent from the client contains data */
            if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
            {
                if ( $sWhere == "" )
                {
                    $sWhere = "WHERE ";
                }
                else
                {
                    $sWhere .= " AND ";
                }



/* RANGE FILTER CODE - This part is not important to this question but included for completenes */

                $columnFilterValue = mysql_real_escape_string($_GET['sSearch_' . $i]);
                // check for values range
                $rangeSeparator = "~";
                if (!empty($rangeSeparator) && strstr($columnFilterValue, $rangeSeparator)) {
                    // get min and max

                     $columnFilterRangeMatches =  explode('~', $columnFilterValue);

                    // get filter
                    if (empty($columnFilterRangeMatches[0]) && empty($columnFilterRangeMatches[1]))
                        $sWhere .= " 0 = 0 ";
                    else if (!empty($columnFilterRangeMatches[0]) && !empty($columnFilterRangeMatches[1]))
                        $sWhere .= $aColumns[$i] . " BETWEEN '" . $columnFilterRangeMatches[0] . "' and '" . $columnFilterRangeMatches[1] . "' ";
                    else if (empty($columnFilterRangeMatches[0]) && !empty($columnFilterRangeMatches[1]))
                        $sWhere .= $aColumns[$i] . " < '" . $columnFilterRangeMatches[1] . "' ";
                    else if (!empty($columnFilterRangeMatches[0]) && empty($columnFilterRangeMatches[1]))
                        $sWhere .= $aColumns[$i] . " > '" . $columnFilterRangeMatches[0] . "' ";
                } else {



    /* Begin building WHERE clause */


        $sWhere = "WHERE (";

                $aORs = array();

                    for ( $i=0 ; $i<count($aColumns) ; $i++ )
                            {
                                if ( $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
                                        {
                                                    $value = $_GET['sSearch_'.$i];

                                                        array_push($aORs, $aColumns[$i]." IN ($value)");


                                        }
                            }
                $sWhere .= implode(" OR ",$aORs);   

        $sWhere .= ')';
                }
            }
        }
这可以很好地工作,但是我想通过发送另一个字符串,该字符串包含按正确顺序排列的OR和的列表,从而使OR的OR和变得动态

例如,如果$_GET['filtertype']=一个如下字符串:-

OR,OR,AND
然后,它将返回以下值,而不是上述值:

WHERE genre_id IN (1,3,5,6) OR instruments IN (1,2,3) OR emotions IN (4,5,6) OR ratings IN (7,8,9)
正如您在上面的代码中所看到的,我目前正在通过内爆函数将OR注入到我的数组中。(以下重复代码的相关部分)

$sWhere=“WHERE(”;
$aORs=array();

对于($i=0;$i而不是创建where字符串,我将首先创建一个包含where部分的数组

$whereParts = array();
foreach($aColumns as $i => $column)
{
    <logic goes here>
    $whereParts[] = 'genre_id IN (1,3,5,6)'; // sample :)
}

$where = 'WHERE ' . implode(' OR ', $whereParts); // note the spaces around OR
$whereParts=array();
foreach($i=>$column作为$i列)
{
$whereParts[]=“第(1,3,5,6)类中的类型id;”;//示例:)
}
$where='where'。内爆('OR',$whereParts);//注意或周围的空格
然后很容易用
和'


允许用户在和和或之间为所有where部分进行选择是很容易的,但如果您想为每个单独的项目进行选择,这也是一个逻辑问题。当用户指定
a或b和c
时,他会想要
(a或b)和c
还是
a或(b和c)

我不创建where字符串,而是首先创建一个包含where部分的数组

$whereParts = array();
foreach($aColumns as $i => $column)
{
    <logic goes here>
    $whereParts[] = 'genre_id IN (1,3,5,6)'; // sample :)
}

$where = 'WHERE ' . implode(' OR ', $whereParts); // note the spaces around OR
$whereParts=array();
foreach($i=>$column作为$i列)
{
$whereParts[]=“第(1,3,5,6)类中的类型id;”;//示例:)
}
$where='where'。内爆('OR',$whereParts);//注意或周围的空格
然后很容易用
和'


允许用户为所有where部件在和和或之间进行选择很容易,但如果您想为每个单独的项目进行选择,则不需要这样做。这也是一个逻辑问题。当用户指定
a或b和c
时,他会想要
(a或b)和c
还是
a或(b和c)

为什么要重新发明轮子?有很多ORM库可以做到这一点……听起来很傻,但我甚至不知道它们的存在!我是MySQL的新手,老实说,我只需要稍微修改一下我的个人代码。如果你能提出一些比我自己的解决方案更好的建议,那就太好了。:-)我看到了潜在的mysql注入!尝试验证从$\u get获得的值!使用类似于if(preg_match('/^[0-9,]+$/s',$subject)){}的方法check@Spidfire-谢谢,我应该说得更清楚,它们都在别处验证并存储为变量。。。。我刚刚在这里把它缩短了,所以我直接把它放进去了。不过,感谢您提到:-)像由客户机数据填充的代码只是请求SQL注入攻击。您确实需要检查添加到字符串中的每个值。我怀疑CSV行类似于
);从表中删除*;挑选(
真的会毁了你的一天。编辑:如果你在其他地方验证你的数据,那很好,但你真的不应该以当前的形式发布,因为其他人可能会在没有意识到风险的情况下复制你的代码。为什么要重新发明轮子?有很多ORM库可以做到这一点…听起来很愚蠢,但我甚至不知道它们的存在!我是相对的我是MySQL新手,老实说,只需要对我的个人代码稍作修改。如果你能提出一些比我自己的解决方案更好的建议,那就太好了。:-)我看到了潜在的MySQL注入!尝试验证从$\u get获得的值!使用类似If(preg\u match('/^[0-9,]+$/s',$subject)){}到check@Spidfire-谢谢,我应该说得更清楚些,它们都在别处验证并存储为变量…我刚刚在这里把它缩短了,所以我直接把_GET放进去了。不过谢谢你提到:-)像客户端数据填充的代码只是要求SQL注入攻击。您确实需要检查添加到字符串中的每个值。我怀疑CSV行类似于
);从表中删除*;挑选(
真的会毁了你的一天。编辑:如果你在别处验证你的数据,那很好,但你真的不应该以当前的形式发布,因为其他人可能会在没有意识到风险的情况下复制你的代码。好主意,我可能会采用你建议的方法。不过,你的假设是我希望他们能够对所有部分都这样做是正确的d你是对的,我没有仔细考虑逻辑。字符串必须表示哪些部分是正确的好主意,我可能会采用你建议的方法。但是,你的假设是我希望他们能够对所有部分都这样做。你是对的,我没有仔细考虑逻辑。字符串必须表示e哪些零件正确或不正确
$whereParts = array();
foreach($aColumns as $i => $column)
{
    <logic goes here>
    $whereParts[] = 'genre_id IN (1,3,5,6)'; // sample :)
}

$where = 'WHERE ' . implode(' OR ', $whereParts); // note the spaces around OR