Wrox Programmer Forums
|
PHP Databases Using PHP in conjunction with databases. PHP questions not specific to databases should be directed to one of the other PHP forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 7th, 2004, 02:10 PM
Authorized User
 
Join Date: Jul 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajuru Send a message via Yahoo to rajuru
Default database search

Dear I am facing some problem with search string.
I have suppose three fields to enter search query
1. Firstname
2. Lastname
3. Email

I used this statement to search database;
select * from `cs_profile` WHERE firstname='"$_POST['firstname']."%' AND lastname='".$_POST['lastname']."%' AND email='".$_Post['email']."'"
Query is successful.

But, when any one search giving only firstname, remaining lastname and email blank I don't get result that people who signed up filling every fields.

I can understand the problem is when user seach only giving firstname and remaining other fields blank, database also search keeping these columns blank though these columns are not blank.

My Desire:
If anyone give only firstname i want to create querystring
"SELECT * FROM `cs_profile` WHERE firstname='".$_POST['firstname']."'"
if anyone give only lastname i want to create querystring
"SELECT * FROM `cs_profile` WHERE firstname='".$_POST['firstname']."'"


I can manually solve it. But when there are many fields then what I should do.

Best Regard:
Md. Zakir Hossain (Raju)
www.rubd.net
www.xenex.rubd.net
www.forum.rubd.net
__________________
Best Regard:
Md. Zakir Hossain (Raju)

www.rajuru.xenexbd.com - my blog with PHP scripts, PHP Book Review and many more
 
Old November 9th, 2004, 11:17 AM
Authorized User
 
Join Date: Oct 2004
Posts: 84
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to SiliconFuRy
Default

Try this:

$sql = "select * from `cs_profile` WHERE ";

if (!empty($_POST['firstname'])) {
$sql .= "firstname='" . $_POST['firstname'] . "%'";
}
(repeating for the other variables)

You'd need to do some checking of the number of variables passed to determine wether you need an "AND" between the two SQL conditional statements.

Many shoes,

James
 
Old November 9th, 2004, 04:32 PM
Authorized User
 
Join Date: Jul 2004
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to rajuru Send a message via Yahoo to rajuru
Default

Dear James
thank for reply,

but before your reply i tried this code:

$sstring="";
if(!empty($_POST['firstname'])) $sstring ="firstname LIKE '%" .$_POST['firstname'] ."%' OR ";
if(!empty($_POST['lastname'])) $sstring .="lastname LIKE '%".$_POST['lastname']."%' OR ";
if(!empty($_POST['nickname'])) $sstring .="nick LIKE '%".$_POST['nickname']."%' OR ";
if(!empty($_POST['district'])) $sstring .="district LIKE '".$_POST['district']."' OR ";
if(!empty($_POST['************'])) $sstring .="************ LIKE'" . $_POST['************']."' OR ";
if(!empty($_POST['dept'])) $sstring .="dept LIKE '" .$_POST['dept']."' OR ";
if(!empty($_POST['session'])) $sstring .="session LIKE '" .$_POST['session']."' OR ";
if(!empty($_POST['yearofpass'])) $sstring .="yearofpass LIKE '" .$_POST['yearofpass']."'";

$sstring=rtrim($sstring," OR");

and it is working very nice

Best Regard:
Md. Zakir Hossain (Raju)
www.rubd.net
www.xenex.rubd.net
www.forum.rubd.net





Similar Threads
Thread Thread Starter Forum Replies Last Post
Search in Database????? Samora C# 2 October 10th, 2007 07:41 AM
how to search in database saif44 ASP.NET 2.0 Professional 11 March 8th, 2006 03:33 PM
need help, to Search from database vikky17 Classic ASP Basics 1 February 5th, 2006 12:35 AM
Search Database bspradeep Classic ASP Databases 1 September 15th, 2004 05:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.