Current location: Hot Scripts Forums » Programming Languages » PHP » mysql searching


mysql searching

Reply
  #1 (permalink)  
Old 10-19-07, 03:42 PM
wambulance wambulance is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
mysql searching

say i have a table in mysql and in that table has the name "john, doe" in the name field. so in the search form someone types john, doe but nothing shows because when it send it to the view results page it has this in the url www.something.com/search.php?s=john%2+doe. so on the search page nothing shows up. i'm use the SELECT to find things and im using LIKE to find it. any help please because.
Reply With Quote
  #2 (permalink)  
Old 10-19-07, 04:16 PM
Flamer Flamer is offline
New Member
 
Join Date: Oct 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Use the urldecode function:

PHP Code:

<?php

echo urldecode("john%2C+doe");
?>
Hope this helps.
Reply With Quote
  #3 (permalink)  
Old 10-19-07, 08:44 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
If your search string looks like this: "john, doe".
Then this:
www.something.com/search.php?s=john%2+doe,
should look like this:
www.something.com/search.php?s=john%2C+doe

You are using the GET method.
and you should be receiving the data using:
PHP Code:

$some_variable $_GET["s"]; 

And $some_variable should be used in your query
PHP Code:

"SELECT * FROM table_name WHERE name LIKE '$some_variable'" 

Or like this:
PHP Code:

"SELECT * FROM table_name WHERE name LIKE '".$_GET["s"]."'" 

I setup the same scenario in my database, and it works just fine.

If you can't figure it out from this information, then you will need to post
your code if you want us to figure out the problem.
__________________
Jerry Broughton

Last edited by job0107; 10-19-07 at 08:49 PM.
Reply With Quote
  #4 (permalink)  
Old 10-19-07, 09:32 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by Flamer View Post
Use the urldecode function:

PHP Code:

<?php
echo urldecode("john%2C+doe");
?>
Hope this helps.
Please read:
Quote:
A reminder: if you are considering using urldecode() on a $_GET variable, DON'T!

Evil PHP:

<?php
# BAD CODE! DO NOT USE!
$term = urldecode($_GET['sterm']);
?>

Good PHP:

<?php
$term = $_GET['sterm'];
?>

The webserver will arrange for $_GET to have been urldecoded once already by the time it reaches you!

Using urldecode() on $_GET can lead to extreme badness, PARTICULARLY when you are assuming "magic quotes" on GET is protecting you against quoting.

Hint: script.php?sterm=%2527 [...]

PHP "receives" this as %27, which your urldecode() will convert to "'" (the singlequote). This may be CATASTROPHIC when injecting into SQL or some PHP functions relying on escaped quotes -- magic quotes rightly cannot detect this and will not protect you!

This "common error" is one of the underlying causes of the Santy.A worm which affects phpBB < 2.0.11.
__________________
Jerry Broughton
Reply With Quote
  #5 (permalink)  
Old 10-23-07, 09:01 PM
wambulance wambulance is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
thanks that worked but what if they leave a field blank?
Reply With Quote
  #6 (permalink)  
Old 10-24-07, 02:57 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
What exactly do you mean?
A field in the table?
A field in the form?
Or what?

Could you please be very specific in your explanation.
__________________
Jerry Broughton
Reply With Quote
  #7 (permalink)  
Old 10-24-07, 07:51 PM
wambulance wambulance is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
if they left a field blank in the form so when they send it, its just blank on the insert.php
Reply With Quote
  #8 (permalink)  
Old 10-24-07, 07:56 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
As far as I can tell, it shouldn't do anything. It just won't return any results.

But if you are having a problem, then post your code, along with a explanation
of the problem.
__________________
Jerry Broughton
Reply With Quote
  #9 (permalink)  
Old 10-24-07, 08:04 PM
wambulance wambulance is offline
Newbie Coder
 
Join Date: Sep 2005
Posts: 55
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<?php



mysql_connect
("localhost""root""") or die(mysql_error());
mysql_select_db("Order") or die(mysql_error());

$name $_GET[newname];
$buy $_GET[newbuy];
$month $_GET[newmonth];

$result mysql_query("SELECT * FROM stuff WHERE name LIKE '$name'
AND buy LIKE '
$buy'
AND month LIKE '
$month'
"
)

or die(
mysql_error());  

$num_rows mysql_num_rows($result);
If(
$num_rows == 0)
{
 echo 
"No items found.";
 }
else
{

then below ihave it being shown. my problem is if someone leaves the $month blank then it shows nothing but i want it so maybe they everything by that one person. or just by one thing they ordered(not a ecommerence(SP) site. or maybe if they only enter in 2 fields instead of 3 it shows everything that they entered but not the field they left blank.
Reply With Quote
  #10 (permalink)  
Old 10-24-07, 08:29 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Now I see your dilemma.

You could try using OR instead of AND in your query.
Or you can use different queries for different conditions.
Or you could use RLIKE and regex expressions in your query.
But I don't know regex very well.

If you choose to try regex, then maybe you can coax mab into
helping you.
__________________
Jerry Broughton
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
Exclude digit from searching MySQL Database Mythvn Database 0 10-06-07 09:02 PM
Searching mysql database blinn_shade Database 2 09-30-07 03:25 PM
Searching in mysql database Dan Man Database 3 07-19-07 10:56 AM
searching MySQL via php?? DolphinTuna PHP 2 01-25-06 04:36 PM


All times are GMT -5. The time now is 07:29 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.