Database Structure Image showing database structure
Database view Image showing database view
Query ExecutionQuery running perfectly fine when searching normal text
Failed query Query unable to fetch results when searching unicode text
I am facing a peculiar problem. I am storing my query in a variable where it is being constructed on run time though various search parameters being passed from the previous form(13 parameters to be precise). The query constructed is looking for unicode data stored in the database.
Sample of query constructed, which is stored in $query variable is SELECT * from orders WHERE Court LIKE '%PBR%' AND App_Name LIKE '%काशी%'
the code for the query construction is given below:
$mysqli_link= new mysqli("localhost", "root", "", "revenue");
mysqli_set_charset( $mysqli_link, 'utf8');
if($_POST['id_sflag']=='1')
{
// define the list of fields
$fields = array('CaseNO', 'Yr', 'CaseType', 'Court', 'Dt_Disposal', 'App_Name', 'Res_Name', 'Pet_Cou_Name', 'Res_Cou_Name', 'District', 'Pro_Off_Name', 'Division', 'Tehsil');
$conditions = array();
}
// builds the query
$query = "SELECT * from orders";
// loop through the defined fields
foreach($fields as $field){
// if the field is set and not empty
if(isset($_POST[$field]) && !empty($_POST[$field])) {
// create a new condition while escaping the value inputed by the user (SQL Injection)
$conditions[] = "$field LIKE '%" . mysqli_real_escape_string($mysqli_link, $_POST[$field]) . "%'";
}
}
// if there are conditions defined
if(count($conditions) > 0) {
// append the conditions
$query .= " WHERE " . implode (' AND ', $conditions);
}
echo "$query";
$result = mysqli_query($mysqli_link, $query);
$num_rows=mysqli_num_rows($result);
mysqli_close($mysqli_link);
Now the problem starts :
When I run the query above which the search criteria like PBR is met it is executed and finds the result. But when I include a wildcard search parameter like app_name it does not give any result whereas there is data in the table. If I copy / paste the query and run it in PHP Myadmin i get the results. The code to display the results is given below:
$i=1;
$tot=0;
while($row = mysqli_fetch_array($result))
{
$dis=$row['District'];
$div=$row['Division'];
$teh=$row['Tehsil'];
$cnt1=$row['Page_Cnt'];
$dyear=$row['Dis_year'];
$filen=$row['CCY'];
$newfile=$string = str_replace("/","_",$filen);
$newfile1="judgements/"."$dyear"."/"."$newfile".".pdf";
$Appname=$row['App_Name'];
$resname=$row['Res_Name'];
$disposal=$row['Dt_Disposal'];
$officer=$row['Pro_Off_Name'];
$pcnt=$row['Page_Cnt'];
printf("
<tr>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\">%s / %s / %s</td>
<td class=\"sub6\">%s</td>
<td class=\"sub6\"><a href=\"%s\">View Order</a></td>
</tr>\n",$i,$filen,$Appname,$resname,$disposal,$officer,$dis,$div,$teh,$pcnt,$newfile1);
$tot=$tot+$cnt1;
$i=$i+1;
}