【问题标题】:MySQL. Insert UTF-8 characters to databasemysql。将 UTF-8 字符插入数据库
【发布时间】:2013-05-05 06:26:04
【问题描述】:

我需要将 Facebook 中的用户名添加到数据库,这些名称包含 UTF-8 字符,例如 (ą,č,ę,ė,į,š,ų,ū)。当我将它添加到数据库时,例如字母 š 看起来像 Å¡

在 myPhpAdmin 中,我找到了设置编码 UTF-8 的表设置,我这样做了,但同样的问题。 我在代码中添加了mysql_set_charset('utf8');,但这也无济于事。代码现在看起来像:

<?php 
    $time = $_POST['time'];
    $username = $_POST['userName'];

    session_start();
    $name = $_SESSION['vardas']; 
    $times = gmdate('H:m:s', $time);

    mysql_set_charset('utf8');
    $mysqli = new mysqli("localhost","my_db","pass","my_db");
if ($stmt = $mysqli->prepare("INSERT into eurokos (time, userName) VALUE (?,?) ")) {

$stmt->bind_param('is', $time, $name);
  // $stmt->bind_param("s", $username);

   $stmt->execute();

   if ($stmt->error != '') {
       echo ' error:'.$stmt->error;
   } else {
       echo 'success';
   }
   $stmt->close();
} else {
   echo 'error:'.$mysqli->error;
}

此 php 文件也转换为 UTF-8 编码,但无济于事。

当我使用此代码echo "Name: " . $user_profile['name']; Facebook 正确打印用户名和 UTF-8 中的普通字母时。所以问题在于将用户名插入数据库。

我用来将用户名变量从 fb.php 发送到 db.php(也许这里我需要使用任何编码?)

$name = $user_profile['name'];
session_start();
$_SESSION['vardas'] = $name;

你能帮帮我吗?谢谢。

【问题讨论】:

  • mysql_set_charset 适用于如果您使用的是 mysql 扩展;您正在使用 mysqli 并且必须使用相应的 mysqli 方式来设置连接字符集。
  • 我收到错误:Fatal error: Call to a member function set_charset() on a non-object... 使用时:$mysqli-&gt;set_charset("utf8");

标签: mysql database encoding utf-8 character-encoding


【解决方案1】:

这解决了我的问题:

if (!$mysqli->set_charset("utf8")) {
    printf("Error loading character set utf8: %s\n", $mysqli->error);
} else {
    printf("Current character set: %s\n", $mysqli->character_set_name());
}

【讨论】:

    猜你喜欢
    • 2017-05-14
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 2019-10-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多