note 73421 added to function.PDO-prepare
| From: | johniskew at osu1 dot php dot net | Date: | Thu, 22 Feb 2007 16:03:06 +0000 |
| Subject: | note 73421 added to function.PDO-prepare | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-123716@lists.php.net to get a copy of this message | ||
If you need to create variable sql statements in a prepare statement...for example you may need to
construct a sql query with zero, one, two, etc numbers of arguments...here is a way to do it without
a lot of if/else statements needed to glue the sql together:
<?php
public function matchCriteria($field1=null,$field2=null,$field3=null) {
$db=DB::conn();
$sql=array();
$paramArray=array();
if(!empty($field1)) {
$sql[]='field1=?';
$paramArray[]=$field1;
}
if(!empty($field2)) {
$sql[]='field2=?';
$paramArray[]=$field2;
}
if(!empty($field3)) {
$sql[]='field3=?';
$paramArray[]=$field3;
}
$rs=$db->prepare('SELECT * FROM mytable'.(count($paramArray)>0 ? ' WHERE
'.join(' AND ',$sql) : ''));
$result=$rs->execute($paramArray);
if($result) {
return $rs;
}
return false;
}
?>
----
Server IP: 64.71.164.2
Probable Submitter: 158.118.51.2 (proxied: 1.0 ISAPROXY)
----
Manual Page -- http://www.php.net/manual/en/function.PDO-prepare.php
Edit -- https://master.php.net/note/edit/73421
Del: integrated -- https://master.php.net/note/delete/73421/integrated
Del: useless -- https://master.php.net/note/delete/73421/useless
Del: bad code -- https://master.php.net/note/delete/73421/bad+code
Del: spam -- https://master.php.net/note/delete/73421/spam
Del: non-english -- https://master.php.net/note/delete/73421/non-english
Del: in docs -- https://master.php.net/note/delete/73421/in+docs
Del: other reasons-- https://master.php.net/note/delete/73421
Reject -- https://master.php.net/note/reject/73421
Search -- https://master.php.net/manage/user-notes.php