| | |
Generate sql string to query MySQL DB ?
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved |
Hi folks.
I Have a question :
I'm interested how you generate query string to search DB with more than 1 form with LIKE.
For example :
We have $_POST['name'], $_POST['title'], $_POST['phone'], $_POST['email']. How do you generate sql query to search with two criteria ( name and phone ) ?
Thanks.
- Mitko Kostov.
I Have a question :
I'm interested how you generate query string to search DB with more than 1 form with LIKE.
For example :
We have $_POST['name'], $_POST['title'], $_POST['phone'], $_POST['email']. How do you generate sql query to search with two criteria ( name and phone ) ?
Thanks.
- Mitko Kostov.
Last edited by MitkOK; Jul 12th, 2007 at 7:47 am.
Yes, that's right. But what if $_POST['name'] is not set ? And what if there are more fields to check ?
Now think that we have 15 fields to check. Is there an easy way to check ( loop maybe ) ?
php Syntax (Toggle Plain Text)
if (isset($_POST['name'])) { $query.="WHERE name LIKE '%$_POST'name']'"; } else { $query.="AND name LIKE '%$_POST['name']'"; } if (isset($_POST['title'])) { $query.="WHERE title LIKE '%$_POST['title']'"; } else { $query.="AND title LIKE '%$_POST['title']'"; } if (isset($_POST['email'])) { $query.="WHERE email LIKE '%$_POST['email']'"; } else { $query.="AND email LIKE '%$_POST['email']'"; }
Now think that we have 15 fields to check. Is there an easy way to check ( loop maybe ) ?
Last edited by MitkOK; Jul 12th, 2007 at 8:40 am.
•
•
Join Date: Sep 2006
Posts: 162
Reputation:
Solved Threads: 14
this is really rough but i think you mean something along these lines
PHP Syntax (Toggle Plain Text)
$sql = "select * from table name where "; $start; if(isset $_POST['name']) { $sql = $sql . $_POST['name']; $start = 1; } if(isset $_POST['phone']) { if($start ==1) { $sql = $sql . "and '". $_POST['name'] ."'"; } else { $sql = $sql . $_POST['name']; } } $sql = $sql . ";";
This I wrote a function for my purpose, it simple but works for me :
PHP Syntax (Toggle Plain Text)
function genQuery() { $row_names = array("name","email","phone","title"); $post_values = array($_POST['name'], $_POST['email'], $_POST['phone'], $_POST['title']); $num = count($post_values); $sqlString = "SELECT * FROM t WHERE"; for ($i=0;$i<$num;$i++) { if (empty($post_values[$i])) { $sqlString.=""; } if (($post_values[$i]) && ($i==0)) { $sqlString.=" $row_names[$i] LIKE '%$post_values[$i]%'"; } if (($post_values[$i]) && ($i!=0)) { $sqlString.=" $row_names[$i] LIKE '%$post_values[$i]%'"; } } $sqlString = preg_replace("/%'/", "%' AND ", $sqlString); $len = strlen($sqlString); $len=$len-4; $sqlString = substr($sqlString, 0 , $len); return $sqlString; }
Last edited by MitkOK; Jul 12th, 2007 at 12:13 pm.
![]() |
Other Threads in the PHP Forum
- Previous Thread: File Uploads
- Next Thread: If URL Exists
Views: 4883 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cookies cron curl database date directory display download dynamic ebooks echo email error file files folder form forms function functions google href htaccess html image include insert integration ip java javascript joomla jquery js limit link login loop mail menu methods mlm mod_rewrite multiple mysql oop parse paypal pdf php problem query radio random recursion regex remote script search select server sessions sms soap source space speed sql stored structure subdomain syntax system table tutorial update updates upload url validation validator variable video web xml youtube





