•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 391,559 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,743 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 1904 | Replies: 10 | Solved
![]() |
•
•
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation:
Rep Power: 2
Solved Threads: 10
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 6:47 am.
•
•
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation:
Rep Power: 2
Solved Threads: 10
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 7:40 am.
•
•
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation:
Rep Power: 2
Solved Threads: 10
•
•
Join Date: Sep 2006
Posts: 162
Reputation:
Rep Power: 2
Solved Threads: 14
this is really rough but i think you mean something along these lines
$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 . ";";•
•
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation:
Rep Power: 2
Solved Threads: 10
•
•
Join Date: Jul 2007
Location: Sofia, Bulgaria
Posts: 138
Reputation:
Rep Power: 2
Solved Threads: 10
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 11:13 am.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb PHP Marketplace
- Previous Thread: File Uploads
- Next Thread: If URL Exists


Linear Mode