0

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;
}
  • Why are you using mysql are you that behind? Use pdo –  Mar 29 '18 at 06:36
  • https://stackoverflow.com/questions/1650591/whether-to-use-set-names – Noman Mar 29 '18 at 06:36
  • **The `mysql` PHP extension is dead** -- Stop using the [`mysql_*()` PHP functions](http://php.net/manual/en/function.mysql-connect.php) in new code. They are old, deprecated since PHP 5.5 and completely removed in PHP 7. Use [`mysqli`](http://php.net/manual/en/book.mysqli.php) or [`PDO_mysql`](http://php.net/manual/en/ref.pdo-mysql.php) instead. Read the answers to [this question](https://stackoverflow.com/q/12859942/4265352) to learn more about why and how. – axiac Mar 29 '18 at 06:45
  • `mysql_*` is no longer maintained in newer version of `PHP`. try to move toward latest `php 7` version along with `mysqli_*` or `PDO` library. – Alive to die - Anant Mar 29 '18 at 06:47
  • Windows XP and Mysql_* functions... What year is it? AmitSharma. You may want to try what @axiac suggested. – Keval Domadia Mar 29 '18 at 12:37

1 Answers1

0

Mysql query is now outdated use mysqli query or you can use PDO method. I have given a mysqli query example to fetch data from mysql server.

        $sql="SELECT * FROM commentview WHERE postid = $postid order by comid DESC LIMIT $count;";
        $result=mysqli_query($db,$sql);//$db is the connection string
        if(mysqli_num_rows($result)>0){

            while($row = mysqli_fetch_assoc($result))
            {   

                $variable = $row['coulmnname'];//column name as in database
            } 
        }
    else
    {
       echo 'Result not Found';
    }

Hope, it will help you. Happy Coding..

Yashdeep Raj
  • 1,006
  • 10
  • 22
  • Agreed, i may be using the outdated codes, but still can anyone tell why i am not getting the results.. Where i am wrong – Amit Sharma Mar 29 '18 at 08:17
  • mysql functions doesn’t work above PHP 5.4 and you can use mysqli functions. check which version you are using. If this is not the issue, check if you are successfully connecting with Database or not. I hope by then your issue will be resolved – Yashdeep Raj Mar 29 '18 at 11:42
  • I have now changed the code to mysqli. The problem still remains.Pls. check the see the converted code. I think that the problem lies with the query that it is not able to search the data which is saved in unicode format in the table. Could you please offer some help – Amit Sharma Mar 29 '18 at 12:18
  • set collation to utf-8 for the tables. Query is supported. You may want to change a little bit and see how mysqli functions work. Have you tried to var_dump $result before checking the count? Also, try running the same query in PHPMYadmin, if it fails there too then you to change the collation to utf8, else I guess you are fine. – Keval Domadia Mar 29 '18 at 12:41
  • My tables are set to utf8_general_ci collation. Even the field is utf8_general_ci. The query works fine when copied from the browser and pasted in PhpMyAdmin. I think the problem lies in the unicode data which is being passed on through the query. Somewhat it is not being parsed correctly.
    Any suggestions.
    The database structure and the view are attached as images in the problem.
    – Amit Sharma Mar 30 '18 at 05:40