Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/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
Php mysql动态查询不带where子句_Php_Mysql - Fatal编程技术网

Php mysql动态查询不带where子句

Php mysql动态查询不带where子句,php,mysql,Php,Mysql,在下面的示例中,有一个基本查询。可以动态添加其他参数以完成查询 但是,我的基本查询没有WHERE子句 最好的解决办法是什么 例如,如果我在基本查询中使用,其中1=1,它似乎可以解决问题,但我怀疑这是正确的解决方案 $myQuery = "SELECT fr.oranges, fr.aplles, fr.bananas, FROM fruits fr LEFT JOIN countrys ct ON fr.id_fruit = ct.id_fruit

在下面的示例中,有一个基本查询。可以动态添加其他参数以完成查询

但是,我的基本查询没有WHERE子句

最好的解决办法是什么

例如,如果我在基本查询中使用,其中1=1,它似乎可以解决问题,但我怀疑这是正确的解决方案

$myQuery = "SELECT fr.oranges, fr.aplles, fr.bananas,
            FROM fruits fr
            LEFT JOIN countrys ct ON fr.id_fruit = ct.id_fruit";


if(!empty($countrys){
     $myQuery .= " AND countrys = ? ";
}

 if(!empty($sellers){
     $myQuery .= " AND seller = ? ";
}

 $myQuery .=" GROUP BY fr.id_fruit ORDER BY fr.fruit ASC";

编辑:我修复了从$empty到empty的写入间隔。

您可以使用数组控制SQL,如下所示:

$where = [];
if(!$empty($countrys){
    $where[] = " countrys = ? ";
}

if(!$empty($sellers){
    $where[] = " seller = ? ";
}

if(count($where) > 0) {
    $myQuery .= " WHERE ".implode('AND', $where);
}
$myQuery = "SELECT fr.oranges, fr.aplles, fr.bananas,
            FROM fruits fr
            LEFT JOIN countrys ct ON fr.id_fruit = ct.id_fruit";

$where = [];
if(!empty($countrys){
     $where[] = "countrys = ?";
}

if(!empty($sellers){
     $where[] = "seller = ?";
}

if (!empty($where)) {
    $myQuery .= " WHERE " . implode(" AND ", $where);
}

$myQuery .= " GROUP BY fr.id_fruit ORDER BY fr.fruit ASC";

可以使用数组控制SQL,如下所示:

$where = [];
if(!$empty($countrys){
    $where[] = " countrys = ? ";
}

if(!$empty($sellers){
    $where[] = " seller = ? ";
}

if(count($where) > 0) {
    $myQuery .= " WHERE ".implode('AND', $where);
}
$myQuery = "SELECT fr.oranges, fr.aplles, fr.bananas,
            FROM fruits fr
            LEFT JOIN countrys ct ON fr.id_fruit = ct.id_fruit";

$where = [];
if(!empty($countrys){
     $where[] = "countrys = ?";
}

if(!empty($sellers){
     $where[] = "seller = ?";
}

if (!empty($where)) {
    $myQuery .= " WHERE " . implode(" AND ", $where);
}

$myQuery .= " GROUP BY fr.id_fruit ORDER BY fr.fruit ASC";

WHERE 1=1
是一种非常简单的hack,它可以很好地工作,因为它简化了代码。有一篇很好的文章解释了
的性能含义,其中1=1
。普遍的共识是,这不会对绩效产生影响

另外,稍微注意(
$empty
)可能不是您定义的函数。我想你想要。你可以这样写:

$where = [];
if(!$empty($countrys){
    $where[] = " countrys = ? ";
}

if(!$empty($sellers){
    $where[] = " seller = ? ";
}

if(count($where) > 0) {
    $myQuery .= " WHERE ".implode('AND', $where);
}
$myQuery = "SELECT fr.oranges, fr.aplles, fr.bananas,
            FROM fruits fr
            LEFT JOIN countrys ct ON fr.id_fruit = ct.id_fruit";

$where = [];
if(!empty($countrys){
     $where[] = "countrys = ?";
}

if(!empty($sellers){
     $where[] = "seller = ?";
}

if (!empty($where)) {
    $myQuery .= " WHERE " . implode(" AND ", $where);
}

$myQuery .= " GROUP BY fr.id_fruit ORDER BY fr.fruit ASC";

WHERE 1=1
是一种非常简单的hack,它可以很好地工作,因为它简化了代码。有一篇很好的文章解释了
的性能含义,其中1=1
。普遍的共识是,这不会对绩效产生影响

另外,稍微注意(
$empty
)可能不是您定义的函数。我想你想要。你可以这样写:

$where = [];
if(!$empty($countrys){
    $where[] = " countrys = ? ";
}

if(!$empty($sellers){
    $where[] = " seller = ? ";
}

if(count($where) > 0) {
    $myQuery .= " WHERE ".implode('AND', $where);
}
$myQuery = "SELECT fr.oranges, fr.aplles, fr.bananas,
            FROM fruits fr
            LEFT JOIN countrys ct ON fr.id_fruit = ct.id_fruit";

$where = [];
if(!empty($countrys){
     $where[] = "countrys = ?";
}

if(!empty($sellers){
     $where[] = "seller = ?";
}

if (!empty($where)) {
    $myQuery .= " WHERE " . implode(" AND ", $where);
}

$myQuery .= " GROUP BY fr.id_fruit ORDER BY fr.fruit ASC";

检查问题1。不再需要WHERE子句。2.添加其中1=1表示同意。感谢Ali、ryantxr和SYMCBEAN检查问题1。不再需要WHERE子句。2.加上WHERE 1=1是可以的,谢谢Ali,ryantxr和SYMCBean这确实让生活简单多了——但你甚至可以只使用“WHERE 1”这确实让生活简单多了——但你甚至可以只使用“WHERE 1”Obrigado Filipe.Obrigado Filipe。