简体   繁体   中英

Select query returned empty result for my indian language

I have mysql table like below

图片

https://photos.app.goo.gl/MWuakdf1daibihEHA

When I use

SELECT * 
  FROM dict 
 WHERE malayalam_definition = '$_value'

it returned empty row but if I use

SELECT * 
  FROM dict 
 WHERE english_word = '$_value'

I get values.

I use SET NAMES utf8 still no luck.

I used utf8mb4 character set and utf8mb4_general_ci collation

Edit: First I created database with utf8mb4_general_ci collation and created table with same collation. Then I import csv file with

load data local infile '/mnt/c/Users/justi/Desktop/enml/enml.csv' 
into table dict 
CHARACTER SET utf8mb4
fields terminated by '\t' 
IGNORE 1 ROWS;

php file

<?php
$dbhost = "localhost";
$dbuser = "newuser";
$dbpass = "password";
$dbname = "dict3";

//Connect to MySQL Server
$c = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$quer = "SET NAMES utf8mb4";
mysqli_query($c, $quer);

$wpm = $_GET['wpm'];

$query="SELECT * FROM dict WHERE malayalam_definition = '$wpm'";

$qry_result = mysqli_query($c, $query) or die(mysqli_error($c));
if(mysqli_affected_rows($c)>0) {
  echo "done";
} else {
  echo "no";
}


while($row = mysqli_fetch_array($qry_result)) {


   echo $row['english_word']."<br />";
   echo $row['part_of_speech']."<br />";
   echo $row['malayalam_definition']."<br />";

}


?>

+--------+----------------+----------------+---------------------------------+
| # id   | english_word   | part_of_speech | malayalam_definition            |
+--------+----------------+----------------+---------------------------------+
| 174569 | .net           | n              | പുത്തന്‍ കമ്പ്യൂട്ടര്‍ സാങ്കേതികത ഭാഷ      |
+--------+----------------+----------------+---------------------------------+
| 116102 | A bad patch    | n              | കുഴപ്പം പിടിച്ച സമയം               |
+--------+----------------+----------------+---------------------------------+
| 219752 | a bag of bones | phr            | വളരെയതികം മെലിഞ്ഞ വ്യക്തി അഥവാ മൃഗം |
+--------+----------------+----------------+---------------------------------+

I use id as primary key.

What's the value of $_value you're trying to find in this code block?

SELECT * 
  FROM dict 
 WHERE malayalam_definition = '$_value'

If the value does not match any values in the malayalam_definition column then there will really be no result.

Try debugging by checking if the values in malayalam_definition column can really be read by your query, just by selecting all the values from the malayalam_definition column:

SELECT malayamam_definition from dict;

If values are returned, try to copy one value from it and assign it to your $_value and try to query again your first query

/*replace $copiedValueFromTheReturnedValues with your copied value from the ```malayalam_definition``` column's returned values */
$_value = $copiedValueFromTheReturnedValues;

SELECT * from dict WHERE malayalam_definition = '$_value';

and try to debug from here.

EDIT

Please note that character set should be utf8 and utf8_unicode_ci collation.

I think your while loop should be configured too. Add MYSQLI_ASSOC parameter inside your mysqli_fetch_array() to get an associative array rows and index them by table column name:


while($row = mysqli_fetch_array($qry_result, MYSQLI_ASOC)) {


   echo $row['english_word']."<br />";
   echo $row['part_of_speech']."<br />";
   echo $row['malayalam_definition']."<br />";

}

After editing the charset, collation and while loop, this query of yours should work now. :)

SELECT * 
  FROM dict 
 WHERE malayalam_definition = '$_value'

EDITED to answer your question:

Is it correct usage while($row = mysqli_fetch_array($qry_result)) { if(in_array($wpm, $row)) { echo "found"; break; } else { echo "Not found"; break; } }

To correct the code, should be:

while($row = mysqli_fetch_array($qry_result, MYSQLI_ASSOC)) {
  if($wpm === $row['malayalam_definition']) {
    echo "found";
    break;
  } else {
    echo "Not found";
    break;
  }
}

What I did is I added MYSQLI_ASSOC as parameter for your mysqli_fetch_array(). I also replaced your line if(in_array($wpm, $row)) to if($wpm === $row['malayalam_definition']) since you're iterating thru each row. $wpm should be compared to each row's 'malayalam_definition' value.

Hope this helps. Let me know what problem you encountered. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM