[英]Multiple sort using SQL SELECT Query using PHP and Form
我使用带有6个标准的html表单,在这里的变量中使用$_POST
lat的转换标准:
案例1 - 所有标准都是默认的
$core = null; $mhz = null; $ram = null; $cam = null; $mAh = null $screen = null
正确的SQL查询是这样的:
$sql = "SELECT * FROM $tbl_name ORDER BY performanta_cpu_core DESC, performanta_cpu DESC, performanta_rami DESC LIMIT $start, $limit";
案例2 - 只设置了一个标准
$core = null; $mhz = "performanta_cpu=1400"; $ram = null; $cam = null; $mAh = null $screen = null
核心查询是这样的:
$sql = "SELECT * FROM $tbl_name WHERE $mhzz ORDER BY performanta_cpu_core DESC, performanta_cpu DESC, performanta_rami DESC LIMIT $start, $limit";
案例3 - 这是问题所有或不止一个标准矿石设置:
$core = 2; $mhz = "performanta_cpu=1400"; $ram = "performanta_rami=1024"; $cam = "camera_spate=3.2"; $mAh = "baterie_mAh=2250"; $screen = "densitate=441";
我知道我需要在设置任何变量的同时使“ WHERE
”变为dinamic和visible,并且我还需要一个“ AND
”,也可以像din:
$sql = "SELECT * FROM $tbl_name WHERE $core AND $mhzz ORDER BY performanta_cpu_core DESC, performanta_cpu DESC, performanta_rami DESC LIMIT $start, $limit";
我有一个星期的压力,没有帮助我无法前进...
提前致谢
免责声明:这是一个糟糕的代码,有一百万个更好的方法来做到这一点,但是,这是最简单的解释。
$parameters = array();
if(!empty($core)){
$parameters['core'] = $core;
}
if(!empty($mhz)){
$parameters['mhz'] = $mhz;
}
if(!empty($ram)){
$parameters['ram'] = $ram;
}
if(!empty($cam)){
$parameters['cam'] = $cam;
}
if(!empty($mAh)){
$parameters['mAh'] = $mAh;
}
if(!empty($screen)){
$parameters['screen'] = $screen;
}
$sql = "SELECT * FROM $tbl_name WHERE 1=1 ";
foreach($parameters as $k=>$v){
$sql .= " AND ".$k."='".$v."'";
}
$sql .= " ORDER BY performanta_cpu_core DESC, performanta_cpu DESC, performanta_rami DESC LIMIT $start, $limit";
// All of those parameters should be sanitized to prevent SQL injection.
// mysql_* is deprecated, use mysqli_* or PDO.
格式很好的问题..干得好。
我可能会错误地解释这个问题,但我认为你在问如何动态构建一个查询。 也许你不知道你连字符串?
例如。
if ($core != null) {$query.= 'AND core ='.$core;}
我希望这会让你朝着正确的方向前进。
从查询的开头开始,并使命令更容易构建添加“始终为true”的WHERE条件:
$sql = "SELECT * FROM $tbl_name WHERE 1=1";
然后浏览你的变量并根据需要添加到WHERE条件(AND之前的空间非常重要):
if ($core) $sql .= " AND performanta_cpu_core = '$core'";
if ($mhz) $sql .= " AND whatever = '$mhz'";
... and so on for the other four variables
然后附加你的ORDER BY和LIMIT,你就完成了(ORDER BY之前的空间非常重要):
$sql .= " ORDER BY performanta_cpu_core DESC, performanta_cpu DESC, performanta_rami DESC LIMIT $start, $limit";
另外,正如Mahmut指出的那样,你不能拥有WHERE $mhz
,你必须拥有WHERE your-column-name = $mhz
。
确保首先从MySQL命令行或WorkBench中尝试查询。 这将帮助您使用语法,包括哪些列需要单引号,哪些不需要。
这不是组装查询的理想方式,但它可以工作,看起来你刚刚开始使用PHP / MySQL,所以不需要立刻给你太多。
$parameters = array();
if(!empty($core)){
$parameters[] = "core = '$core'";
}
if(!empty($mhz)){
$parameters[] = "mhz = '$mhz'";
}
if(!empty($ram)){
$parameters[] = "ran = '$ram'";
}
if(!empty($cam)){
$parameters[] = "cam = '$cam'";
}
if(!empty($mAh)){
$parameters[] = "mAh = '$mAh'";
}
if(!empty($screen)){
$parameters[] = "screen = $screen";
}
if (empty($parameters)) {
$whereclause = "";
} else {
$whereclause = "WHERE " . join(' AND ', $parameters);
}
$sql = "SELECT * FROM $tbl_name $whereclause ORDER BY performanta_cpu_core DESC, performanta_cpu DESC, performanta_rami DESC LIMIT $start, $limit";
您可以使用array_filter
和implode
来构建where
子句
$conditions = array_filter(array($core, $mhz, $ram, $cam, $mAh, $screen));
$clause = implode(' and ', $conditions);
这使所有非空元素$conditions
,然后连接这些与and
。 然后你可以用它作为
'... where ' . $clause . '...'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.