简体   繁体   中英

Unicode characters properly pulled from MySQL [PHP MYSQL]

I am fetching the image filepath from my MySQL database, and everything worked fine until the name of the path has some unicode characters associated to it. For example, I am using this code to fetch the image filepath;

$sql = "SELECT images FROM agents";
$basePath = "http://localhost/img/";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)) {
    $img = $row['images'];
    $actualPath = $basePath . $img;
    echo $actualPath;
}

For English alphabet pictures, it's okay, it turned out something like http://localhost/img/selena.jpg but if it's using unicode characters, it will be like http://localhost/img/11_??.jpg whereas the true filepath would be http://localhost/img/11_副本.jpg
How do I make that 副本 characters stay after it is pulled from MySQL rather than just becoming ?? symbol ?

EDIT: I used a prepared statement method, not the basic mysqli_query() command.

Use SET NAMES UTF8 before your query to handle Unicode characters.

mysqli_query($con, 'SET NAMES UTF8');

$sql = "SELECT images FROM agents";
$basePath = "http://localhost/img/";
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)) {
   $img = $row['images'];
   $actualPath = $basePath . $img;
   echo $actualPath;
}

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