因此,我对 PDO 真的很陌生,我正在尝试一些东西并对其进行练习,并且碰巧一旦尝试了,我就对数据库所发生的事情感到非常困惑在下面的代码中插入一些特殊字符:
<?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!?? | | |
+------------------+------------+-------------+----------------+-----------------------------------------+---------------------------+----------------+
*/
?>
我正在寻找以下答案:为什么这个字符(“''” )没有存储在数据库中,而是被(?代替了。 >)相反,我尝试将表和列更改为UTF8,因为我已经阅读了社区的一些答案,但似乎没有什么区别,尽管它可以与(áéíóú)配合使用。预先感谢!
根据Madhur Bhaiya的建议,我再次尝试检查它的字符集,甚至将charset=utf8mb4
附加到我的PDO DSN上,这对我有很大帮助,而不是每个使用utf8_decode
或utf8_encode
字符串,并且确实已经将其全部设置为UTF8,所以我猜想此字符集不支持这种类型的字符,例如卷曲引号,那么我该如何解决呢?