Best way to compare strings
Moderator: General Moderators
Best way to compare strings
I have a database with all the cities of my region in it and the details of that city.
I want people to be able to search the database by entering the name of their city.
Of course I could use:
SELECT * FROM cities WHERE city='name_they_entered';
But that wouldn't always be good, cause there's some cities that are often spelled wrong such as:
- "Knokke-Heist", sometimes people spell it "Knokke Heist", sometimes "Knokkeheist" etc.
What's the best way to search the database in your opinion?
I want people to be able to search the database by entering the name of their city.
Of course I could use:
SELECT * FROM cities WHERE city='name_they_entered';
But that wouldn't always be good, cause there's some cities that are often spelled wrong such as:
- "Knokke-Heist", sometimes people spell it "Knokke Heist", sometimes "Knokkeheist" etc.
What's the best way to search the database in your opinion?
That's probably your best bet, but perhaps an easier to implement solution would be FULLTEXT indexing. If you create a FULLTEXT index on the 'city' column, then with the appropriate query, MySQL will return all the cities that are similar in spelling, in order of relevancy.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
I always provide them a regular text input...
But if the input contains % or _ i generate a query that uses LIKE instead of =
But the FULLTEXT search is very appealing too
(Should give it a try after exams...)
But if the input contains % or _ i generate a query that uses LIKE instead of =
Code: Select all
if (strpos($_POST['place'], '%') !== false || strpos($_POST['place']) !== false)
{
$query .= "place LIKE '" . mysql_escape_string($_POST['place']) . "'";
}
else
{
$query .= "place = '" . mysql_escape_string($_POST['place']) . "'";
}Well, the difference is that mysql_real_escape_string() uses the current connection and escapes the string based on that connection's character set. I guess they deprecated mysql_escape_string() to improve coding practices and kind of force everyone to handle different character sets.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
<excuse>
I usually depend on ADODB to do this for me
</excuse>
(While i was typing the example, i wondered if i would type mysql_real_escape_string or mysql_escape_string... I choose for the second, because i was too lazy to type _real_ and provide a link to the db
Next time i'll be less lazy and more consistent 
I usually depend on ADODB to do this for me
</excuse>
(While i was typing the example, i wondered if i would type mysql_real_escape_string or mysql_escape_string... I choose for the second, because i was too lazy to type _real_ and provide a link to the db