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
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 assuming what you want / want to do. Use one to ensure that you get all the words from your query like this: Now in $ matches [0] you have your results: Now use it to create an SQL statement using either 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. This should do the trick for you - if I get you right. To use them, use the jobtitle_keywords.keywords
field
$ re = '/ (\ w +) / I '; $ Str = 'Science / Head of Mathematics'; Preg_match_all ($ re, $ str, $ matches);
array ("head", "of", "science", "math")
. . and
or OR
. You can do this easily.
$ queryAddon = "jobtitle_keywords.keywords" like '%'. Implode ("% 'and jobtitle_keywords.keywords like'% ', $ matches [0])"%' ";
Comments
Post a Comment