php - Building dynamic queries which contains between clause -
i need generate dynamic queries in program based on search criteria. commonly using following method. need add between clause this.
$firstname = 'aaa'; $lastname = 'bbb'; $fromdate = '2015-01-01'; $todate = '2015-01-31'; $query = 'select * users'; $cond = array(); $params = array(); if (!empty($firstname)) { $cond[] = "firstname = ?"; $params[] = $firstname; } if (!empty($lastname)) { $cond[] = "lastname = ?"; $params[] = $lastname; } if (!empty($fromdate) && !empty($todate)) { // here need add between $fromdate , $todate } if (count($cond)) { $query .= ' ' . implode(' , ', $cond); } $stmt = $pdo->prepare($sql); $stmt->execute($params);
is there way add between clause code?
just add clause before executing.
if (count($cond)) { $query .= ' ' . implode(' , ', $cond); } if (!empty($fromdate) && !empty($todate)) { $query .= ' col_field between ? , ?'; $params[]= $fromdate; $params[]= $todate; } $stmt = $pdo->prepare($sql); $stmt->execute($params);
Comments
Post a Comment