Forum Moderators: open

Message Too Old, No Replies

Mysql search query problem

         

chrisrock79

5:27 pm on Apr 13, 2006 (gmt 0)

10+ Year Member



Hello all,
This is my first post here so I'd like to introduce myself first. My name is Chris and I do quite a bit of html/xhtml and css.
I'm trying to learn php/mysql now and have run into a problem while creating a search.
The database I am accessing is mysql.

This is my search query

"SELECT * FROM Product, Picture, CategoryProduct WHERE CategoryProduct.Enabled = 1 AND CategoryProduct.ProductID = Product.ProductID AND ((Product.Title LIKE '$srch') ¦¦ (Product.ShortDescription LIKE '$srch')) AND Picture.EntityID = Product.ProductID";

Every now and then I get duplicate results when the search is returned. It doesn't happen with every search just certain ones. I have checked the database and the products which are duplicates do not appear twice in the table. What causes this to happen? (I am using if(number of row >=1 ){do{results}while()} )

And also, I am a greenie at this, so looking at my query, is it done in the most efficient manner?
tblCategoryProduct is only used to check if the product is enabled. tblProduct contains all the product info, of course and tblPicture contains the urls to the images, if that information helps at all.

I appreciate any insight anyone may have.

Thanks for your time and I apologize if this is quite elementary.

Chris

txbakers

9:00 pm on Apr 13, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi and welcome to the webmaster world!

most likely you are getting duplicates from a join that you are missing. in your WHERE clause, you might need to specify another match or two to make sure you are getting unique records.

if it's possible to get more than one result, you could look into the DISTINCT parameter after SELECT, which will only bring back unique records.

chrisrock79

5:48 am on Apr 14, 2006 (gmt 0)

10+ Year Member



Hi and thanks for the welcome.

After looking more into it I was starting to think my Join isn't specific enough. I'm starting from a book which doesn't go into much detail with Joins.

Thanks for the help,

Chris