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.

7
  • You have to check the character set in PHP and mysql and the file saved name. Commented Dec 23, 2016 at 6:50
  • is it display as 副本 in your db? Commented Dec 23, 2016 at 7:49
  • @Beginner yes it does. Commented Dec 23, 2016 at 8:48
  • @AfiqIzzat try $actualPath = $basePath . htmlentities($img, ENT_COMPAT, 'UTF-8'); Commented Dec 23, 2016 at 8:54
  • @Beginner, the result still remains the same, it outputs 坿¬ instead of 副本 Commented Dec 23, 2016 at 8:59

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Does it work the same if I used prepared statement ?
Now, it becomes http://localhost/img/11_坿¬.jpg, I still want it to become 副本
Can you post how this image link is stored in the DB?
I stored only the filepath of the image and call it on another server for the picture by just relaying the filepath (basepath + imgpath). The filename is 11_副本.jpg.
when you query through terminal or any tool, does your image value in the DB look exactly like 11_副本.jpg ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.