简体   繁体   English

使用php变量创建动态mysql查询

[英]Create a dynamic mysql query using php variables

I have an html table that loads everything in a mySQL database table. 我有一个html表,可将mySQL数据库表中的所有内容加载。 I have dropdowns that relate to columns of that mySQL table - when the user selects one of the dropdowns it uses AJAX to query the database. 我有与该mySQL表的列相关的下拉菜单-当用户选择下拉菜单之一时,它将使用AJAX查询数据库。

I need to figure out how to build the query dynamically because sometimes the dropdowns will be empty (ie they don't want to filter by that column). 我需要弄清楚如何动态构建查询,因为有时下拉列表将为空(即,它们不想按该列进行过滤)。

What is the best way to do this? 做这个的最好方式是什么?

Currently I have something like this: 目前我有这样的事情:

    $stationFilter = $_GET['station'];
    $verticalFilter = $_GET['vertical'];
    $creativeFilter = $_GET['creative'];
    $weekFilter = $_GET['week'];    

    $result = mysql_query("SELECT * FROM $tableName WHERE STATION_NETWORK = '$stationFilter' AND VERTICAL = '$verticalFilter' AND CREATIVE = '$creativeFilter'  AND WK = '$weekFilter'");   
    $data = array();
    while ($row = mysql_fetch_row($result) )
        {
        $data[] = $row;
        }   
    $finalarray['rowdata'] = $data;

Which you can imagine doesn't work because if any of those fields are empty - the query fails (or returns nothing, rather). 您可以想象这是行不通的,因为如果这些字段中的任何一个为空,查询都会失败(或者不返回任何内容)。

Obviously creating such a 'static' query like that really makes it difficult if certain variables are empty. 显然,创建这样的“静态”查询确实会使某些变量为空的困难。

What is the best way to dynamically create that query so that it only enters the ones that are not empty get added to the query so it can successfully complete and display the appropriate data? 动态创建该查询以使其仅输入不为空的查询的最佳方法是添加到查询中以便成功完成并显示适当的数据?

Just check if the variables contain a value and if they do, build the query like so: 只需检查变量是否包含值,如果它们包含,就可以像下面这样构建查询:

unset($sql);

if ($stationFilter) {
    $sql[] = " STATION_NETWORK = '$stationFilter' ";
}
if ($verticalFilter) {
    $sql[] = " VERTICAL = '$verticalFilter' ";
}

$query = "SELECT * FROM $tableName";

if (!empty($sql)) {
    $query .= ' WHERE ' . implode(' AND ', $sql);
}

echo $query;
// mysql_query($query);
$filter = array();

if ($_GET['station'] != '')
{ $filter[] = 'STATION_NETWORK = '.$_GET['station'];}
if ($_GET['vertical'] != '')
{ $filter[] = 'VERTICAL = '.$_GET['vertical'];}
if ($_GET['creative'] != '')
{ $filter[] = 'CREATIVE  = '.$_GET['creative'];}
if ($_GET['week'] != '')
{ $filter[] = 'WK = '.$_GET['week'];}

$query = 'SELECT * FROM $tableName WHERE '.implode(' AND ', $filter);
$result = mysql_query($query);
... 

but better if in GET you pushed name of tables rows; 但是更好,如果在GET中推送了表名行; $_GET['STATION_NETWORK'] --- some like this; $ _GET ['STATION_NETWORK'] ---像这样;

then you make
foreach ($_GET as $key => $value)
{
    if ($value != '')
    { $filter[] = $key.' = '.$value;}
}

or try 或尝试

$filter = array('STANTION_NETWORK' => $_GET['station'],
                'VERTICAL' => $_GET['vertical'],
                 'CREATIVE'  => $_GET['creative'],
                 'WK' => $_GET['week']);
$query_array = array();

 foreach ($filter as $key => $value)
{
    if ($value != '')
    { $query_array[] = $key.' = '.$value;}
}
$query = 'SELECT * FROM $tableName WHERE '.implode(' AND ', $query_array);
$result = mysql_query($query);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM