MySQLi: Non-English Characters

2019-08-15 04:16发布

问题:

I have a Table named "status" Which is used to store objects status. The table has 5 columns and except "symbol" column which it's type is VARCHAR utf8mb4_unicode_ci, All the others are just typical integers.

The problem is that because the symbol column can store English and Non-English characters, It returns NULL(With var_dump($row)) When I use my php file "getStatus.php" to retrieve the data from a row with Non-English symbol but It works well when I use English characters as symbol.

This is my table(the symbol on the second row is "تست"):

This is the php code, "getStatus.php" :

if(!isset($_GET["symbol"]))
        die("please specify the symbol");
$con = mysqli_connect("localhost","root","******","****");
// Check connection
if (!$con) {
    die("Connection failed: " . mysqli_connect_error());
}
$query = "SELECT * FROM `status` WHERE `symbol` =  '" . $_GET["symbol"] . "';";
$result = mysqli_query($con,$query);
if (!$result)
        die("unable to retreive status");
$row = mysqli_fetch_assoc($result);
if ($row["status"] == 1)
        echo $row["percentage"];
else
        echo -1;

I can successfully get data When I execute getStatus.php?symbol=EU which calls mysql with

SELECT * FROM `status` WHERE `symbol` = 'ER';

But When I execute getStatus.php?symbol=تست, I got null! The confusing point is that if I copy the query and enter it in mysql command line It will work but it doesn't work when I send the same query with mysqli in php!!

SELECT * FROM `status` WHERE `symbol` = 'تست';

Why it works in mysql commandline and phpmyadmin but not with mysqli?

回答1:

Use html_entity_decode():

Use html_entity_decode() on your $_GET["symbol"] value

Instead of

$query = "SELECT * FROM `status` WHERE `symbol` =  '" . $_GET["symbol"] . "';";

run

$query = "SELECT * FROM status WHERE symbol = 'html_entity_decode([$_GET["symbol"])'";

This solution worked here



标签: php mysql mysqli