php - Use REGEX in user input variable in SQL Query. -


I want to write a SQL query with a class that looks for user input values ​​in the database table.

Say a job title to a job title "Head of Science and Mathematics"

The query should look at all the words in the separate job title and accordingly the list of return departments.

  $ keyword = mysql_real_escape_string (keyword $); Select departments.name from departments, jobtitle_keywords where regexp '$ keyword' jobtitle_keywords.keywords and departments.id = jobtitle_keywords.dept_id;  

This query runs perfectly in ideal conditions (a particular character, spaces, etc.), but otherwise fails

Does anyone give me all kinds of Can specifically guide the characters and discrepancies to guide them in order to avoid specials so that my query does not fail.

Any help is greatly appreciated.

Thanks

"itemprop =" text ">

Then in your comments, I think that you have your user search input ( i.e. "head of science / mathematics" ) and only search for words ( head, science, and mathematics ) and in your To use them, use the jobtitle_keywords.keywords field

assuming what you want / want to do.

Use one to ensure that you get all the words from your query like this:

  $ re = '/ (\ w +) / I '; $ Str = 'Science / Head of Mathematics'; Preg_match_all ($ re, $ str, $ matches);  

Now in $ matches [0] you have your results: array ("head", "of", "science", "math") . .

Now use it to create an SQL statement using either and or OR . You can do this easily.

  $ queryAddon = "jobtitle_keywords.keywords" like '%'. Implode ("% 'and jobtitle_keywords.keywords like'% ', $ matches [0])"%' ";  

gives you 'code%' as 'job% title' jobtitle_keywords Like keywordskeywords and '% of%', jobtitle_keywords.keywords and thus '% science%' and jobtitle_keywords.keywords like '% math%' .

So now just put your question in that and fire it up.

From the "departments, jobtitle_keywords select different departments.name". " WHERE department.id = jabitill_keywordDrapt_ID "." And (". $ QueryAddon.") ";

This should do the trick for you - if I get you right.


Comments

Popular posts from this blog

Pass DB Connection parameters to a Kettle a.k.a PDI table Input step dynamically from Excel -

multithreading - PhantomJS-Node in a for Loop -

c++ - MATLAB .m file to .mex file using Matlab Compiler -