I have written a mysqli statement for advanced search of my website. the generated query does not work. here is the generated query:
SELECT title, stars, pic_main, id, province FROM ads WHERE 'province' = 8 AND( title LIKE %شریف% OR content LIKE %شریف% OR keywords LIKE %شریف% OR title LIKE %شریف%)
And here is the code that I use to execute the query:
if($transorder = $site_db->query($statement))
{
echo "True";
$i=0;
while($row_obj = $transorder->fetch_object())
{
$item[$i]['id'] = $row_obj->id;
$item[$i]['pic_main'] = $row_obj->pic_main;
$item[$i]['title'] = $row_obj->title;
$item[$i]['province'] = $row_obj->province;
$item[$i]['stars'] = $row_obj->stars;
$i++;
}
}
else
{
echo "False";
}
You need quotes around text in your LIKE statements, also I notice you are using title 2 times. If you use single quote (province), MySQL might expect you are comparing the String 'province' with the value 8
you should use ` around the 'province'
:
SELECT title, stars, pic_main, id, province
FROM ads
WHERE `province` = 8
AND ( title LIKE '%شریف%'
OR content LIKE '%شریف%'
OR keywords LIKE '%شریف%'
OR title LIKE '%شریف%')
To find out what error MySQL is returning you can use echo $site_db->error;
.
The Solution was
$site_con->set_charset("utf8");
Because of the use of the use of none default characters of the MySQL connection. So the connection should be set to utf8