简体   繁体   English

如何将此mySql查询转换为PDO格式...?

[英]How to convert this mySql query to PDO format…?

How should i write my PDO Prepare and bindValue/param statement for this type of query where i check whether the value is not null then only add it to the query string ..... 我应该如何为这种类型的查询编写我的PDO Prepare和bindValue / param语句,其中我检查该值是否不为null,然后仅将其添加到查询字符串中。

$query = "SELECT * FROM cabs WHERE DATE='$date' ";
if ($mode!=='' || $mode!=="")
  $query .="AND MODE='$mode' ";

if ($tfno!=='')
  $query .="AND TFNO='$tfno' ";
  $query .="ORDER BY TIME";

Quick answer, without testing: 快速答案,无需测试:

<?php
$params = array(':date' => $date);
$query = "SELECT * FROM cabs WHERE DATE=':date' ";
if ($mode!=='' || $mode!=="") {
  $query .="AND MODE=':mode' ";
  $params[':mode'] = $mode;
}

if ($tfno!=='') {
  $query .="AND TFNO=':tfno' ";
  $params[':tfno'] = $tfno;
}
$query .="ORDER BY TIME";

$req = $dbh->prepare($query);
$req->execute($params);

Just push in the param array each time your query gets more filters, and using a name should be easier, I'm not sure that array_push would preserve the order, so .. 只需在每次查询获得更多过滤器时都推入param数组,并且使用名称应该更容易,但我不确定array_push是否会保留顺序,所以..

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

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