I'm using latin special characters on my database. For exemple:
The word "FLÁVIO" is saved like
flávio
I'm trying to use LIKE to search this item.. for example.. Users search for "Flávio" and I would like to return "flávio".
I tried to do something like this:
$search = $_POST['search'];
$search = htmlentities($search);
$sql = "SELECT * FROM table WHERE column LIKE '%$search%';
The conversion is done, but when I put this $search on the SQL Query it doesn't match.
How can I fix? Thanks.
Just an idea, but have you tried using REGEXP?
$sql = "SELECT * FROM table WHERE column REGEXP '$search'";
I wonder if that will make a difference.
Related
My table look like this:
Table screenshot
Here I'm getting the result by query:
$subject_ids = implode(',', $_POST['subject_ids'])
SELECT * FROM table WHERE focusarea LIKE '%$subject_ids%' ;
The result is perfect, but there is nothing to display when I select more than one subject ids, like if selecting only one then it shows,
but when to select 1, 2, and 4, but there is nothing with this LIKE query...
How can I fix this?
Use implode like,
PHP
$subject_id_aray = explode(",",$_POST['subject_ids']);
$in_array_string = array();
foreach($subject_id_aray as $values){
$in_array_string[] = "'".$values."'";
}
MySql
$sql = "SELECT * FROM table WHERE focusarea in (".implode(",",$in_array_string).") ;";
LIKE clause will not work in your case because using LIKE '%1,2,3%' in query will not get anything, as you as using Ids you should use IN instead of LIKE. LIKE will be used separately for each id if it is string.
As you are getting $_POST['subject_ids'] as an array, query will be like
$subject_str = implode(',', $_POST['subject_ids']);
$sql = "SELECT * FROM table WHERE focusarea IN($subject_str)";
If your column focusarea is not integer then
$subject_str = "'".implode("','", $_POST['subject_ids'])."'";
$sql = "SELECT * FROM table WHERE focusarea IN($subject_str)";
Maybe you have bug in POST.
Try to echo, $subject_ids befor inject to SQL.
You focus are is simple string of numbers, connected by ,, but what you are sending by POST maybe is not correct.
Other problem, this don't look like you full code.
Provide you file, if this don't resolve problem.
I am facing some problems in MySQL query in PHP.
I have the below Bengali words in a column named 'keywords' and they are assigned to 'গৌরিপ্রসন্ন মজুমদার' value of another column named 'name' in a table named 'lyricist'
সয়না,রয়না,কেমন,তাকে,আমার,তুমি,কৃষ্ণ,রাধে,দিতে,চাই,আরো,কাছে,তোমার,আমার,তুমি,থাকো,কাছে,ডাকো,আমি,পড়েছে
When I'm searching for some consecutive words like 'তোমার,আমার,তুমি' I get the correct answer 'গৌরিপ্রসন্ন মজুমদার' But when i'm searching for random words like 'তোমার,তুমি' it's not giving any answer.
$query = "SELECT name FROM `lyricist` WHERE keywords LIKE '%$val%'";
Here $val has 'তোমার,আমার,তুমি' or 'তোমার,তুমি' these type of values.
Why not it's treating the item randomly? Please HELP.
$query = "SELECT name FROM `lyricist` WHERE keywords LIKE '%$val%'";
Will get you all the matching columns that start with some text having your value and then end with some text.So if you put some random words and they are not in the correct order obviously this query will not match.
I think what you want to do is when you have multiple values is put them in different variable val1,val2,... and using AND in your query
$query = "SELECT name FROM `lyricist` WHERE keywords LIKE '%$val1%' AND keywords LIKE '%$val2%'";
So you would have to construct your query dynamically something like this would work:
<?php
$val="keyword1,keyword2,keyword3";
$keywords=explode(',',$val);
if(!empty($keywords)){
$query="SELECT name FROM `lyricist` WHERE keywords LIKE '%$keywords[0]%' ";
if(count($keywords)>1){
for($i=1;$i<count($keywords);$i++){
$query.="AND keywords LIKE '%$keywords[$i]%' ";
}
}
}
?>
I've been using the function LIKE in this statement however its not doing what I expected for it to do, Basically a user on my site has a subject which usually looks like this hello welcome to my #room hows it going #fun and what im using is the LIKE function to select all users with the subject containing #fun my statement looks like this;
$search = 'fun';
$sql = "SELECT * FROM `usr_users` WHERE `subject` LIKE '%$search%'";
$result = mysql_query($sql);
while ($row = mysql_fetch_assoc($result)) {
echo $row['username'];
}
however when the query runs it only selects the users which have fun at the begining of their subject or not at all. is there a different function I can use to select words from within in the subject not just the first word.
You also need a % at the beginning of the string to search.
$sql = "SELECT * FROM `usr_users` WHERE `subject` LIKE '%$search%'";
--- UPDATE ---
It may be failing you because you may need to escape the data. check for errors mysql_error() against your query. It might be throwing you something about it. mysql_real_escape_string(). That # could be the culprit if it's part of your actual query. Or use htmlspecialchars() or something like that to echo out the query.
I'm new in php. I can search through one keyword not by two. I want to search using multiple words e.g "first+experience"
while entry in database feild is as "First_Experience"
I use this query:
$query = "SELECT * FROM fh_Names WHERE fh_Sid LIKE '%$searchTerm%'";
Also this query is for single table, I want to search data through multiple tables. (joining or some otherway). Please tell me query
I mean User name from fh_Name table and its activity from fh_Activity table
Then explode the string and try this
$str = "first+experience";
$expl = explode("+", $str);
$implode = (",", $expl);
$query = "SELECT * FROM fh_Names WHERE fh_Sid IN($implode)";
I am trying to make a search program. I have three tables: postivewords, negativewords and recommendationwords. These tables consist only of word_id then the word. How I do it in a query? This is what I have so far. Please correct me if I am wrong.
if(isset($_POST['searchword']))
{
$word = $_POST['search'];
$search1= mysql_fetch_array(mysql_query("SELECT * FROM positivethesaurus where word like '%$word%'"));
$search2= mysql_fetch_array(mysql_query("SELECT * FROM negativethesaurus where word like '%$word%'"));
$search3= mysql_fetch_array(mysql_query("SELECT * FROM recommendationthesaurus where word like '%$word%'"));
}
select * from negativethesaurus, positivethesaurus,recomendationthesaurus where negativethesaurus.word like '%"word%' or positivethesaurus.word like '%word%' or recomendationthesaurus.word like '%word%';
This may be not the fastest way (you would use indexes and freetext) but it will be down on only one query.
Oh yes, that been said this query is vulnerable to sql injection attacks.
also do mysql_escape_string to avoid sql injection
$word = $_POST['search'];
$word = mysql_escape_string($word);
$query = SELECT positivethesaurus .*,negativethesaurus.*,recommendationthesaurus.* FROM positivethesaurus,negativethesaurus,recommendationthesaurus where positivethesaurus.word like '%$word%' OR negativethesaurus.word like '%$word%' OR recommendationthesaurus.word like '%$word%'";
UPDATES
if you want to check if results not found then do it like this
<?php
$query = mysql_query("Your Query Here");
$rowCount = mysql_num_rows($query);
if($rowCount>0) {
// DO YOur STUFF IF RESULTS FOUND
} else {
echo "No Results Found";
}
?>
Please not that mysql_* function should not be used
Hope this works. Not Tested though. Let me know if you find any problem
1) Select the respected column instead of select * from
2) avoid sql injection