简体   繁体   中英

PHP Searching email database for emailaddress and body contents based on arbitrary search query

I am working on an email application. Here is the background:

  • Emailaddresses are stored in an addresses table
  • Message contents are stored in a message_contents table

There is a search bar on the top of the page, and the flow of action would be the user would type the following, for example: Jonathan Kushner programming and it would drill down to a point where essentially in this case it located an email containing Jonathan Kushner in the addresses and the word programming in the message body.

This is where the problem lies. Currently, I'm doing MySQL LIKES against email addresses that match the users query, as well as doing MySQL LIKES against the message contents that match the users query. The problem is,as for our example, its never going to find an email address that matches Jonathan Kushner programming and its not going to match any body contents that say Jonathan Kushner programming .

So, with that being said, what I need to accomplish is being able to somehow recognize the query input and discover whats a name and whats a body content. I can't fathom I'm the first person in the world to separate addresses from body contents in a database, so its surely possible.

Maybe I should set it up so the user has to type either a name or an emailaddress in the first words of the query and then after X words they are able to type in keywords that would be in the contents. I don't really like this idea, and it couldnt work because if the user didnt have a name or an email address to search for the whole theory is busted, but I'm just not sure how to solve this problem.

Another idea is to possibly separate the words and perform logic on each word and combinations of words.

Any help would be greatly appreciated.

I have done something like this for an ecommerce search function, I split the words up and then used a loop to add the words to a query.

WHERE address LIKE '%$search[0]%' OR address LIKE '%$search[1]%' OR contents LIKE '%$search[0]%'

Hope this is useful, I would play about with some queries to see what works before jumping into the code. When you do start code I would recommend splitting the search query into an array of words then using foreach loops to add them to the query.

Have fun and let me know how you get on!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM