0

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.

Afiq Izzat
  • 69
  • 8

1 Answers1

1

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;
}
Samir Selia
  • 7,007
  • 2
  • 11
  • 30