繁体   English   中英

如何在PHP中保存到utf8

[英]how to save to utf8 in php

我想保存中文

如果我没有使用utf8保存,它将为“?” ,因此如何使用utf8保存。

或有其他方法可以保存中文,使其不为“?”。

感谢大家的帮助。

这是我的php代码(互联网上的源代码)

<?php

/*
 * Following code will create a new product row
 * All product details are read from HTTP Post Request
 */

// array for JSON response
$response = array();

// check for required fields
if (isset($_POST['name']) && isset($_POST['price']) && isset($_POST['description'])) {

    $name = $_POST['name'];
    $price = $_POST['price'];
    $description = $_POST['description'];

    // include db connect class
    require_once __DIR__ . '/db_connect.php';



   // connecting to db
    $db = new DB_CONNECT();

    // mysql inserting a new row
    $result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");

    // check if row inserted or not
    if ($result) {
        // successfully inserted into database
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        // echoing JSON response
        echo json_encode($response);
    } else {
        // failed to insert row
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

        // echoing JSON response
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

之前

$result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");

尝试

mysql_query("SET CHARSET utf8");

如果从浏览器中的表单获取数据,则必须将浏览器设置为utf8,如下所示:

<meta charset="UTF-8">

并且您必须设置数据库表

collation to utf8

和您的PHP连接到数据库到UTF8

mysql_query("SET NAMES 'utf8'"); 

暂无
暂无

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

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