简体   繁体   English

使用 PHP 将特殊字符转换为 MySQL

[英]Special Characters into MySQL using PHP

I have set up a table in my database to load records from a json file that include special characters.我已经在我的数据库中设置了一个表来从 json 文件中加载包含特殊字符的记录。 I am using the following PHP code to upload the records.我正在使用以下 PHP 代码上传记录。

<!DOCTYPE html>
<html>
<head>
    <title>Insert Records</title>
</head>

<body>
<h1>Insert OER Records from JSON File</h1>

<h2>Outputs:</h2>

<?php

ini_set('memory_limit','2000M');

$url = 'path/filename.json';
$contents = file_get_contents($url);
$contents = utf8_encode($contents);
$results = json_decode($contents, true);

include 'connection.php'; 
// Counter for number of records (x)
$x = 0;

foreach($results as $key => $value) {
    $x = $x + 1;    

    foreach($value as $k => $v) {

            $type = $value['type'];
            $title = $value['title'];
            $title = str_replace("'", "\'", $title);
            $title = str_replace("(", "\(", $title);
            $title = str_replace(")", "\)", $title);
            $author = $value['author'];
            $author = str_replace("'", "\'", $author);
            $author = str_replace("(", "\(", $author);
            $author = str_replace(")", "\)", $author);
            $link = $value['link'];
            $source = $value['source'];
            $source = str_replace("'", "\'", $source);
            $source = str_replace("(", "\(", $source);
            $source = str_replace(")", "\)", $source);
            $description = str_replace("'", "\'", $description);
            $description = str_replace("(", "\(", $description);
            $description = str_replace(")", "\)", $description);
            $description = $value['description'];
            $base_url = $value['base_url'];
            $isbn_number = $value['isbn_number'];
            $e_isbn_number = $value['e_isbn_number'];
            $publication_date = $value['publication_date'];
            $license = $value['license'];
            $subject = $value['subject'];
            $image_url = $value['image_url'];
            $review = $value['review'];
            $language = $value['language'];
            $loc_collection = $value['loc_collection'];
            $license_url = $value['image_url'];
    }

    $sql = "INSERT INTO oer_search (type, title, author, link, source, description, base_url, isbn_number, e_isbn_number, publication_date, license, subject, image_url, review, language, license_url) VALUES ('$type', '$title', '$author', '$link', '$source', '$description', '$base_url', '$isbn_number', '$e_isbn_number', '$publication_date', '$license', '$subject', '$image_url', '$review', '$language', '$license_url')";


    if (mysqli_query($conn, $sql)) {

    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }

}

echo "This many records were entered into the database: " . $x;
    mysqli_close($conn);

?>
<br/><br/><br/><br/><br/>

</body>
</html>

I have my database set to utf8_general_ci.我将数据库设置为 utf8_general_ci。 However, the records with special characters are not displaying correctly.但是,带有特殊字符的记录无法正确显示。 I am getting results like this MuhÌ£ammad Kurd Ê»AliÌ .我得到这样的结果MuhÌ£ammad Kurd Ê»AliÌ Is there something I am missing?有什么我想念的吗?

You are not using mysqli properly.您没有正确使用 mysqli。 (Not your fault, since the PHP manual doesn't explain it well enough) (不是你的错,因为 PHP 手册解释得不够好)

To open the connection to mysqli you must at least do the following:要打开与 mysqli 的连接,您至少必须执行以下操作:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); // enable error reporting
$conn = new mysqli($servername, $username, $password, $dbname);
$conn->set_charset('utf8');

set_charset() method ensures that the data sent to the DB is using the correct character set. set_charset()方法确保发送到数据库的数据使用正确的字符集。 Without it the data might get mangled up.没有它,数据可能会被破坏。

You also must enable proper mysqli error reporting with this line:您还必须使用此行启用正确的 mysqli 错误报告:

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

Without it you would need to use mysqli_error($conn) to check for errors, which could leak sensitive information to the users if misused.如果没有它,您将需要使用mysqli_error($conn)检查错误,如果误用,可能会将敏感信息泄露给用户。

Do not use utf8_encode() .不要使用utf8_encode() Most of the time you do not need this function.大多数时候你不需要这个 function。 Unless you really know what you are doing, get rid of it.除非您真的知道自己在做什么,否则请摆脱它。

Warning:警告:
utf8_general_ci is a very old and obsolete charset. utf8_general_ci是一个非常古老且过时的字符集。 If you are using MySQL 5.5.3 or later or MariaDB you should be using utf8mb4 charset.如果您使用 MySQL 5.5.3 或更高版本或 MariaDB 您应该使用utf8mb4字符集。 See What is the difference between utf8mb4 and utf8 charsets in MySQL?请参阅MySQL 中的 utf8mb4 和 utf8 字符集有什么区别?

you have to use same encode in your files.您必须在文件中使用相同的编码。

Your database have created in utf8, your php files to and you have use this code in your php file crud.您的数据库已在 utf8 中创建,您的 php 文件已在您的 php 文件 crud 中使用此代码。

    mysqli_query("SET NAMES 'utf8'");
    mysqli_query('SET character_set_connection=utf8');
    mysqli_query('SET character_set_client=utf8');
    mysqli_query('SET character_set_results=utf8');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM