简体   繁体   English

使用PHP和Form使用SQL SELECT Query进行多重排序

[英]Multiple sort using SQL SELECT Query using PHP and Form

I use a html form with 6 criterias, using $_POST lat's convert criterias in variables like here: 我使用带有6个标准的html表单,在这里的变量中使用$_POST lat的转换标准:

Case 1 - All criterias are default 案例1 - 所有标准都是默认的
$core = null; $mhz = null; $ram = null; $cam = null; $mAh = null $screen = null
The correct sql query is this : 正确的SQL查询是这样的:
$sql = "SELECT * FROM $tbl_name ORDER BY performanta_cpu_core DESC, performanta_cpu DESC, performanta_rami DESC LIMIT $start, $limit";

Case 2 - Only one criteria is set 案例2 - 只设置了一个标准
$core = null; $mhz = "performanta_cpu=1400"; $ram = null; $cam = null; $mAh = null $screen = null
The corect query is this : 核心查询是这样的:
$sql = "SELECT * FROM $tbl_name WHERE $mhzz ORDER BY performanta_cpu_core DESC, performanta_cpu DESC, performanta_rami DESC LIMIT $start, $limit";

Case 3 - Here is the problem All or more than one criterias ore setted: 案例3 - 这是问题所有或不止一个标准矿石设置:
$core = 2; $mhz = "performanta_cpu=1400"; $ram = "performanta_rami=1024"; $cam = "camera_spate=3.2"; $mAh = "baterie_mAh=2250"; $screen = "densitate=441";

I understand that i have need to make " WHERE " to be dinamic and visible just when any variable is set and also I have need an " AND " also dinamically like: 我知道我需要在设置任何变量的同时使“ 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";
I am stressed for a week with this and i cant advance without help... 我有一个星期的压力,没有帮助我无法前进...

Thanks in advance 提前致谢

Disclaimer: This is terrible code and there are a million better ways to do this, but, this is the simplest explanation. 免责声明:这是一个糟糕的代码,有一百万个更好的方法来做到这一点,但是,这是最简单的解释。

$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.

Excellently formatted question.. well done. 格式很好的问题..干得好。

I may well be misinterpreting the question, however I think you're asking how to construct a query dynamically. 我可能会错误地解释这个问题,但我认为你在问如何动态构建一个查询。 Perhaps you are not aware you concat strings ? 也许你不知道你连字符串?

Eg. 例如。

if ($core != null) {$query.= 'AND core ='.$core;}

I hope this puts you in the right direction. 我希望这会让你朝着正确的方向前进。

Start with the beginning of your query, and to make the command easier to build add an "always true" WHERE condition: 从查询的开头开始,并使命令更容易构建添加“始终为true”的WHERE条件:

$sql = "SELECT * FROM $tbl_name WHERE 1=1";

Then go through your variables and add to the WHERE condition as needed (the space before the AND is really important): 然后浏览你的变量并根据需要添加到WHERE条件(AND之前的空间非常重要):

if ($core) $sql .= " AND performanta_cpu_core = '$core'";
if ($mhz) $sql .= " AND whatever = '$mhz'";
... and so on for the other four variables

Then append your ORDER BY and LIMIT and you're done (the space before the ORDER BY is really important): 然后附加你的ORDER BY和LIMIT,你就完成了(ORDER BY之前的空间非常重要):

$sql .= " ORDER BY performanta_cpu_core DESC, performanta_cpu DESC, performanta_rami DESC LIMIT $start, $limit";

Also, as Mahmut pointed out, you can't have WHERE $mhz , you've got to have WHERE your-column-name = $mhz . 另外,正如Mahmut指出的那样,你不能拥有WHERE $mhz ,你必须拥有WHERE your-column-name = $mhz

Make sure you try out the query from the MySQL command line or WorkBench first. 确保首先从MySQL命令行或WorkBench中尝试查询。 That will help you with syntax, including which columns need single quotes around them and which ones don't. 这将帮助您使用语法,包括哪些列需要单引号,哪些不需要。

This isn't the ideal way to assemble a query but it'll work, and it looks like you're just getting started with PHP/MySQL so no need to throw you too much at once. 这不是组装查询的理想方式,但它可以工作,看起来你刚刚开始使用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";

You can use array_filter and implode to build your where clause 您可以使用array_filterimplode来构建where子句

$conditions = array_filter(array($core, $mhz, $ram, $cam, $mAh, $screen));
$clause = implode(' and ', $conditions);

This keeps all non null elements as $conditions and then concatenates these with and . 这使所有非空元素$conditions ,然后连接这些与and You can then use this as 然后你可以用它作为

'... where ' . $clause . '...'

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

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