【问题标题】:All values of JSON data are not inserted in MySQL Database (for UTF-8 characters)JSON 数据的所有值都未插入 MySQL 数据库(对于 UTF-8 字符)
【发布时间】:2023-03-25 09:50:01
【问题描述】:

我使用 JSON 从 UTF-8 数据库中获取文本,它现在在一个文件中。当我打印数据时:

توحید در نگاه امام علی (علیه السلام)

但是当我将它插入数据库时​​,我得到了这个错误:

插入错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“�وحید در نگاه امام علی”附近使用正确的语法

这是我的代码

<?php 
header('Content-Type: application/json; charset=utf-8');
$con = mysql_connect("localhost","bbbbbbb","bbbbbbb");
mysql_select_db("db", $con);
if(file_exists('./json/file.json')){
    $jsondata = file_get_contents('./json/file.json');
    $data = json_decode($jsondata, true);
    foreach ($data['nodes'] as $node){
        $data_element = $node['node'];
        $title = $data_element['title'];
        $summary = $data_element['summary'];
        $body = $data_element['body'];
        $id = $data_element['id'];
        print $title."\n";
        $insert = "INSERT INTO main(title) VALUES ($title)";
        mysql_query($insert) or die("Insertion Error: ". mysql_error());
    }
}
else 
    print "File doesn't exist";

数据库及其列都是utf8_general_ci。为什么打印出来的文字和数据库里插入的不一样?

【问题讨论】:

  • 我不明白,它是如何工作的,如果您执行此查询 - “INSERT INTO main(title) VALUES (توحید در نگاه امام علی (علیه السلام))”。是否容易受到 sql 注入的影响?
  • $insert = "INSERT INTO main(title) VALUES ($title)"; 根本行不通——因为$title 周围没有引号。
  • @RickJames 这正是我错过的

标签: php mysql json database utf-8


【解决方案1】:

数据库连接也需要知道它是 utf8。 mysql_set_charset('utf8').

你应该看看mysqli,而不是弃用的mysql_con。

【讨论】:

  • 完美答案,但请您编辑它以包含已弃用的 mysql 扩展的警告
  • 我试过了,但它给了我这个错误Error : (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'در نگاه امام علی (علیه السلام))' at line 1
  • 通过将插入查询修改为$insert = "INSERT INTO main(title) VALUES ('$title')";,它起作用了。谢谢
【解决方案2】:

使用这个

$mysqli = new mysqli("localhost","root","","your_db_name");

$mysqli->query("SET NAMES 'utf8'");

$mysqli->query("SET CHARACTER SET utf8");

【讨论】:

  • 你可以这样做,但是在这种情况下,这个查询将在每个请求上执行,所以 10000 个请求将等于 20000 个额外的查询。最好将你的 sql server 配置为全局使用它,而不是每次都设置它。
  • 在config.php中只设置一次,在页面中包含一次配置
  • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
  • 如果我请求你的页面10次,会执行多少次?
  • 我试过了,但它给了我这个错误Error : (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'در نگاه امام علی (علیه السلام))' at line 1
【解决方案3】:

虽然@ericwenn 和@SahilManchal 提出了有效点,但直接错误是未能引用... VALUES ($title) ... 中的字符串。

不要盲目地将字符串放入查询中。

这可行,但它安全:... VALUES ('$title') ...。它受到称为“sql 注入”的黑客攻击。见real_escape_string

另外,与mysqli连接后,使用

$mysqli->set_charset('utf8');

这比SET NAMES 更可取。

【讨论】:

  • 我试过了,但它给了我这个错误Error : (1064) You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'در نگاه امام علی (علیه السلام))' at line 1
猜你喜欢
  • 2013-05-05
  • 2017-05-14
  • 2012-12-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-14
  • 2019-05-26
  • 2013-03-25
相关资源
最近更新 更多