0

So, I'm really new to PDO, and I was trying some stuff and practicing it, and happens that I became really confuse about what is happening to my database once I'm trying to insert some special characters with this code below:

<?php
    $post_cat     = 4;
    $post_title   = "TESTING";
    $post_author  = "Test";
    $post_image   = "test_image.png";
    $post_content = "“Luo Changan, I grant you five breaths’ time. Show me whether you’re actually such a ‘person’ as you say, or a dog that would ‘beg to surrender!’”";
    $post_tags    = "novel, mga, chinese novel";
    $ID_post_stat = 2;

    $pdo = new PDO("mysql:charset=utf8mb4;hostname=localhost;dbname=cms;", "root", "");
    $stmt = $pdo->prepare('INSERT INTO posts (ID_post_category, post_title, post_author, post_image, post_content, post_tags, ID_post_status) VALUES (?, ?, ?, ?, ?, ?, ?)');

    $stmt->bindParam(1, $post_cat);
    $stmt->bindParam(2, $post_title);
    $stmt->bindParam(3, $post_author);
    $stmt->bindParam(4, $post_image);
    $stmt->bindParam(5, $post_content);
    $stmt->bindParam(6, $post_tags);
    $stmt->bindParam(7, $ID_post_stat);
    $stmt->execute();

    /* data stored:   

    +------------------+------------+-------------+----------------+-----------------------------------------+---------------------------+----------------+
    | ID_post_category | post_title | post_author | post_image     | post_content                            | post_tags                 | ID_post_status |
    +------------------+------------+-------------+----------------+-----------------------------------------+---------------------------+----------------+
    |                4 | TESTING    | Test        | test_image.png | ?Luo Changan, I grant you five breaths? | novel, mga, chinese novel |              2 |
    |                  |            |             |                | time. Show me whether you?re actually   |                           |                |
    |                  |            |             |                | such a ?person? as you say, or a dog    |                           |                |
    |                  |            |             |                | that would ?beg to surrender!??         |                           |                |
    +------------------+------------+-------------+----------------+-----------------------------------------+---------------------------+----------------+
    */
?>

I was looking for answers for why this characters (“ ‘ ’ ”) are not being stored on database and are replaced with (?) instead, I had tried to change the table and column to UTF8 as I had read some answers from the community, but it don't seems to make difference, its working fine with (áéíóú) though. Thanks in advance!


as suggested by Madhur Bhaiya, I had tried again to check on it charset, even aplied charset=utf8mb4 to my PDO DSN what helped me alot instead of using utf8_decode or utf8_encode to each string, and it is indeed all set to UTF8 already, so I guess that this kind of characters like curly quotes aren't supported for this charset, then how can I solve this?

Willian
  • 1
  • 1
  • 2
    Possible duplicate of [UTF-8 all the way through](https://stackoverflow.com/questions/279170/utf-8-all-the-way-through) – Madhur Bhaiya Oct 13 '18 at 11:04
  • What you have is a currly quote, from something like MsWord. Probably `Windows-1252` instead of `UTF8` – ArtisticPhoenix Oct 13 '18 at 11:05
  • “ is a UTF-8 character. I built a [PDO container](https://github.com/Jaquarh/PDOEasy) for anyone to use that has everything configured already. – Jaquarh Oct 13 '18 at 19:28

0 Answers0