mysql query, search

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
truepal20032001
Forum Commoner
Posts: 27
Joined: Mon Jun 25, 2007 8:28 pm

mysql query, search

Post by truepal20032001 »

Hello, i want the sql query to search an address in a database, and if it is registered to give an error, else register it,

Code: Select all

SELECT *
FROM `newsletter`
WHERE `address` = CONVERT( _utf8 '$address'
USING latin1 )
COLLATE latin1_german2_ci
LIMIT 0 , 30
but how can i tell it if it finds it,to give an error, else to continues with the code
ocpamit
Forum Newbie
Posts: 7
Joined: Fri Jul 13, 2007 6:53 am
Location: Punjab, India

Post by ocpamit »

if u are looking to match postal address with the existing one in the database, its hard as you see there are many ways to write an address like:

274 e block sbs nagar

274 e sbs nagar

e block-274 sbs nagar

u can use "like" operator in the query, but it would n't match exact address.

other way u can explode the input address by space and match each of the array element with like operator in the query using while loop. Then you can use AND condition for all query results to match the address. something like that may work...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: mysql query, search

Post by califdon »

truepal20032001 wrote:Hello, i want the sql query to search an address in a database, and if it is registered to give an error, else register it,

Code: Select all

SELECT *
FROM `newsletter`
WHERE `address` = CONVERT( _utf8 '$address'
USING latin1 )
COLLATE latin1_german2_ci
LIMIT 0 , 30
but how can i tell it if it finds it,to give an error, else to continues with the code
You can check mysql_num_rows() function and if it's zero, there was no match. But as ocpamit warned, this is probably unworkable because of the possibility of entering an address in so many ways. It is likely to miss finding a match a high percentage of the time.
Post Reply