【问题标题】:need help php pdo prepare sql需要帮助 php pdo 准备 sql
【发布时间】:2017-09-07 19:51:12
【问题描述】:

我正在开发一个 PDO 功能,该功能允许您根据工具的序列号或文章 ID 执行搜索。但是,我希望能够在 id_art 和 num_serie 之间使用“-”同时搜索两者,例如在我的搜索栏 459999-1 中编写。

我尝试输入“-”但没有结果

public static function findAllByIdArt($id){

    $id='%'.$id.'%';
    $c=base::getConnection();
    $query=$c->prepare("select * from outillage where (id_art like :num_serie) or (num_serie like :num_serie) or (num_serie and id_art like :num_serie '-' :id_art)");
    $query->bindParam(':num_serie',$id,PDO::PARAM_INT);
    $query->bindParam(':id_art',$id,PDO::PARAM_INT);
    $dbres=$query->execute();
    $d=$query->fetchAll();
    $tab=array();
    foreach ($d as $key => $value) {
        $a=new Outillage();
        $a->id=$value['id'];
        $a->num_serie=$value['num_serie'];
        $a->id_art=$value['id_art'];
        $a->article=$value['article'];
        $a->id_doc=$value['id_doc'];
        $a->document=$value['document'];
        $a->ilot=$value['ilot'];
        $a->emplacement=$value['emplacement'];
        $a->liste_tubes=$value['liste_tubes'];
        $a->image=$value['image'];
        $a->image2=$value['image2'];
        $a->image3=$value['image3'];
        $a->image4=$value['image4'];
        $a->image5=$value['image5'];
        $a->nb_utilisation=$value['nb_utilisation'];
        $a->conforme=$value['conforme'];
        $a->dateNonConfo=$value['dateNonConfo'];
        $tab[]=$a;
    }
    return $tab;
}

【问题讨论】:

  • or (num_serie between :num_serie and :id_art or id_art between :num_serie and :id_art 这是您要找的吗?
  • ty 但没有。我想在我的搜索栏中搜索两列,例如:6999-01 6999= 我的数据库中的 id_art 和 01= num_serie。我已经可以搜索 num_serie 或 id_art 但不能同时使用“-”搜索(抱歉我的英语不好)

标签: php mysql pdo sql-like


【解决方案1】:

声明的最后一部分必须如下所示:

(num_serie like :num_serie '-' :id_art and id_art like :num_serie '-' :id_art)"

该列必须后跟值。你不能用column1 and column2 like value

【讨论】:

    【解决方案2】:

    需要注意的两点:

    1. 查询不能num_serie and id_art,您需要明确说明该列应查找的值,而不是一系列列。所以应该是num_serie LIKE 'a' AND id_art LIKE 'a',例如。
    2. 可以使用CONCAT() 函数来连接破折号。这是最好的方法,因为您在查询中使用占位符(您应该这样做!)

    如果我正确理解你的问题,这个查询就是这样做的。

    SELECT * 
    FROM outillage 
    WHERE (id_art LIKE :num_serie) 
       OR (num_serie LIKE :num_serie) 
       OR (CONCAT(num_serie, '-', id_art) LIKE CONCAT(:num_serie, '-', :id_art))
    

    另外,在不使用任何通配符的情况下执行LIKE,您还不如使用等号= 而不是LIKE 运算符来正常比较字符串。

    【讨论】:

    • 谢谢qirel 我试过你的方法,但不起作用。每当搜索 id_art 或 num_serie 作品时
    • 对不起,我不太明白最后一句话。每当他们工作时,然后呢?请你描述得好一点好吗?
    • 对不起,我想说 num_serie 或 id_art 的搜索即使使用最后一个条件 (CONCAT(num_serie, '-', id_art) LIKE CONCAT(:num_serie, '-', :id_art))与 (num_serie 和 id_art 类似:num_serie '-' :id_art)" (对不起我的英语)相反。我搜索了 CONCAT(),我认为这是我需要继续工作的原因
    • 我继续围绕 CONCAT() 寻找解决方案。
    • 我被阻止了,我找不到解决方案搜索一次又一次返回:/
    猜你喜欢
    • 2015-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-31
    相关资源
    最近更新 更多