By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,278 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,278 IT Pros & Developers. It's quick & easy.

MySQL searching

P: 40
Hi all,

I'm trying to write up my first SQL database which I would want to be search able. Now currently I can have it search by First Name and Last Name. My code for the search goes like :
[PHP]$search = "SELECT *
FROM `Customer_info`
WHERE `First Name` LIKE CONVERT( _utf8 '$_POST[f_name]'
USING latin1 )
COLLATE latin1_swedish_ci
AND `Last Name` LIKE CONVERT( _utf8 '$_POST[l_name]'
USING latin1 )
COLLATE latin1_swedish_ci
LIMIT 0 , 30" ; [/PHP]

Now I can search only by First Name and Last Name. If I leave out either I get back blank results.

I tried searching around the net, and tried to logic it out on my own, but I just can't figure out how I can make it so that it can search by which ever field (or fields)has had information entered into it.

Can someone please guide me on how i can solve this either through PHP or SQL?

Feb 10 '08 #1
Share this Question
Share on Google+
3 Replies

Expert 100+
P: 801
Moved to PHP Forum.
Feb 10 '08 #2

Expert 5K+
P: 6,050
I believe you can use 'OR' for this.

$_select = "

I also think you should rename your table rows; replace spaces with underscores:
Expand|Select|Wrap|Line Numbers
  1. 'Row Name' = 'Row_Name'
Also be sure to escape your POST strings to prevent mysql injection!
Feb 10 '08 #3

P: 40
Thanks it works!

Thanks for reminding me about the names, I'll change em later. This is a work in progress so I'll be adding in security features later, like closing sessions, checking user input etc. For now I just need it to work to make a presentation on so I can get the green light to finish everything.
Feb 11 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.