Jump to content

Query better results


johnrb87

Recommended Posts

Hello everyone

 

I have the following QUERY

 

SELECT * FROM `places` WHERE `resort` = '.$_GET['q'].'

 

This works great and the values I have stored in the db are;

 

England

Turkey

USA

Brazil

 

So my QUERY could look like

 

SELECT * FROM `places` WHERE `resort` = 'Turkey'

 

or

 

SELECT * FROM `places` WHERE `resort` = 'USA'

 

The problem I have is that some people are spelling names incorrectly and for example, someone spelt

 

Brazil

like

Brazill

 

and

 

Turkey

like

Turkie

 

I did think of the option of a spell checker, so something like

 

"Did you mean "Turkey""

 

but I don't really want to add a spell checker and there is no where in the page design for that to do.

 

Instead I wanted to find out if there is anyway in which SQL can do a partial lookup, so that even if someone spells a word slight wrong or puts a character in the wrong place, then it would still return a result

 

Is there anyway to get PHP or my QUERY to do this?

 

Thanks all

 

John

Link to comment
https://forums.phpfreaks.com/topic/207411-query-better-results/
Share on other sites

The only thing I could think of would be to query the database for the full string, if that doesn't return any results then take off the last letter and query, if that doesn't return anything then take off another letter etc.

 

So

 

$q = 'Turkie';
"SELECT * FROM `places` WHERE `resort` = '.$_GET['q'].'"

// get the string length of $q
$qlength = strlen($q);
// take off the last letter of $q
$q = substr($q, 0, ($qlength)-1); // This would return 'Turki';
"SELECT * FROM `places` WHERE `resort` = '.$q.'%" // the percentage sign is a wildcard


$qlength = strlen($q);
// take off the last letter of $q
$q = substr($q, 0, ($qlength)-1); // This would return 'Turk';
"SELECT * FROM `places` WHERE `resort` = '.$q.'%"

 

This would then return anything matching 'Turk.....'

 

I'm not sure this would be very efficient though so it may be better to manipulate the search string before putting it through the database.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.