简体   繁体   English

动态构建PHP PDO MySQL查询

[英]Dynamically Building PHP PDO MySQL query

I am converting old-style MySQL/PHP queries on a site. 我正在网站上转换旧式MySQL / PHP查询。 I have a page that has a series of checkboxes. 我的页面上有一系列复选框。 This is submitted, and a query is built based on what checkboxes are checked (there are at least 6 like the following): 提交后,将根据选中的复选框(至少有6个类似以下内容)构建查询:

if (xxxxx) {
    $furthersort=$furthersort."AND age_birth='yes' ";
    }
if (xxxxx) {
    $furthersort=$furthersort."AND age_three='yes' ";
    }

...

$prequery = "SELECT id from products WHERE product_categories LIKE '%$catid%' ".$furthersort."ORDER BY product_name ASC";

I'm trying to move the second part this over to PHP like this: 我正在尝试将第二部分移至PHP,如下所示:

$query = $objDb->prepare("SELECT id from products WHERE product_categories LIKE ? ? ORDER BY product_name ASC");
$params3 = array('%$catid%',$furthersort);
$query->execute($params3); 
while ($row = $query->fetch(PDO::FETCH_ASSOC));

But it's not working. 但这不起作用。 The variables created by the if's are correct, so I'm sure it's because I am missing an understanding of how the prepare portion interprets the information, but I need a push in the right direction. if所创建的变量是正确的,所以我确定是因为我缺少对prepare部分如何解释信息的理解,但是我需要朝着正确的方向努力。

You have two problems. 你有两个问题。 First you can only have one bound argument for the LIKE condition, so you have to state that and the subsequent conditions: 首先,您只能为LIKE条件提供一个绑定参数,因此您必须声明该条件和后续条件:

$query = $objDb->prepare("SELECT id from products WHERE product_categories LIKE ?  AND age_three = ? ORDER BY product_name ASC");

Now you can send two values in the array 现在您可以在数组中发送两个值

$furthersort = 'yes';
$params3 = array("%$catid%", $furthersort);

Now, given that we do not know how you set $furthersort it is hard to come up with something exact for you to use but suffice it to say for each condition you add to the query, you have to add another bound parameter if you plan to continue along the lines of creating dynamic queries. 现在,由于我们不知道如何设置$furthersort ,很难提出确切的使用方法,但是对于要添加到查询中的每个条件来说,它足以说明,如果您计划,则必须添加另一个绑定参数继续创建动态查询。 the logic for doing that is much more complex than I have demonstrated here. 这样做的逻辑比我在这里演示的要复杂得多。

Like Mr. Blanchard pointed out, you seem to have unintentionally added 2 place-holders instead of one in your LIKE clause. 就像Blanchard先生指出的那样,您似乎无意中在LIKE子句中添加了2个占位符,而不是一个。 It should read: 它应显示为:

            <?php
            // RIGHT AFTER THE LIKE YOU HAD 2 PLACE-HOLDERS: ? ? RATHER THAN JUST 1: ?
            if (xxxxx) {
                // YOU ARE CONCATENATING "AND" DIRECTLY TO THE $furthersort VARIABLE WITHOUT A SPACE: WRONG...
                // $furthersort = $furthersort."AND age_birth='yes' ";
                $furthersort    = $furthersort." AND age_birth='yes' ";
            }
        if (xxxxx) {
                // YOU ARE CONCATENATING "AND" DIRECTLY TO THE $furthersort VARIABLE AGAIN WITHOUT A SPACE: WRONG...
                // $furthersort = $furthersort."AND age_three='yes' ";
                $furthersort    = $furthersort." AND age_three='yes' ";
            }

        ...

        $prequery  = "SELECT id from products WHERE product_categories LIKE '%";
        $prequery .= $catid . "%' " . $furthersort. " ORDER BY product_name ASC "; // <== WHITE SPACE IS GRATIS IN MYSQL


        $sql        = "SELECT id from products WHERE product_categories LIKE :CAT_ID ORDER BY product_name ASC";
        $query      = $objDb->prepare($sql);
        // $params3 = array('%$catid%', $furthersort);      <==  VARIABLE INSIDE SINGLE QUOTES!!! YOU MAY USE DOUBLE QUOTES...
        $params3    = array("CAT_ID"=>"%" . $catid . "%" . $furthersort);
        $query->execute($params3); 
        while ($row = $query->fetch(PDO::FETCH_ASSOC));

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

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