Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/236.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/233.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何让php添加utf8字符_Php_Android_Mysql - Fatal编程技术网

如何让php添加utf8字符

如何让php添加utf8字符,php,android,mysql,Php,Android,Mysql,我正试图从我的android应用程序向数据库中添加一个符号。。例如,我想添加一个⊂ B这将保存为一个?B在数据库中。我尝试在android中记录字符串,并正确记录了它,所以我假设问题出在php中,对吗? 数据库中的符号也被正确检索,我只有在从应用程序添加或编辑符号时才遇到问题 这是我的添加php <?php // array for JSON response $response = array(); // check for required fields if (isset($

我正试图从我的android应用程序向数据库中添加一个符号。。例如,我想添加一个⊂ B这将保存为一个?B在数据库中。我尝试在android中记录字符串,并正确记录了它,所以我假设问题出在php中,对吗? 数据库中的符号也被正确检索,我只有在从应用程序添加或编辑符号时才遇到问题

这是我的添加php

<?php



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

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

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

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

// connecting to db
$db = new DB_CONNECT();
// mysql_query("SET NAMES 'utf8'"); -> this needs to be removed

mysql_query("SET NAMES 'utf8'");

// mysql inserting a new row
$result = mysql_query("INSERT INTO glossary(Symbol, Name, Description) VALUES('$symbol', '$name', '$description')");
header("Content-type: application/json; charset=utf-8");

// check if row inserted or not
if ($result) {
    // successfully inserted into database
    $response["success"] = 1;
    $response["message"] = "glossary 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);
}
?>
还有我的数据库连接php

<?php

class DB_CONNECT {

// constructor
function __construct() {
    // connecting to database
    $this->connect();
}

// destructor
function __destruct() {
    // closing db connection
    $this->close();
}

/**
 * Function to connect with database
 */
function connect() {
    // import database connection variables
    require_once __DIR__ . '/db_config.php';

    // Connecting to mysql database
    $con = mysql_connect(DB_SERVER, DB_USER, DB_PASSWORD) or die(mysql_error());

    // Selecing database
    $db = mysql_select_db(DB_DATABASE) or die(mysql_error()) or die(mysql_error());
mysqli_query($link, "SET SESSION CHARACTER_SET_RESULTS =utf8;");
mysqli_query($link, "SET SESSION CHARACTER_SET_CLIENT =utf8;");
    // returing connection cursor
    return $con;
}

/**
 * Function to close db connection
 */
function close() {
    // closing db connection
    mysql_close();
}

}

?>
请有人帮我做这件事。。谢谢大家!

要在php中添加utf-8,可以使用:

header('Content-type: text/plain; charset=utf-8');
在您使用的代码中:mysql_query和mysqli_query为什么只使用一种格式,最好使用mysqli_query,即mysqli函数

最好按照@Fred ii-的建议进行适当的更正。

尝试在连接到DB后运行集合名称“utf8”和集合字符集“utf8”

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARSET 'utf8'");

您将mysqli_uu与mysql_uu函数混合在一起;为什么?使用header的内容类型:text/html;字符集=utf-8';那么您就有了或diemysql\u错误或diemysql\u错误?!在一个人倒下的时候开枪,嗯?还要确保你的词汇表的列设置为utf8排序规则way@Fred-我很抱歉,但可以帮助纠正我的代码,因为我不太熟悉mysql。。拜托?你完全确定这就是OP的密码有问题吗?你可能没有读过我的评论,或者没有完全阅读OP的代码。@Fred ii-没有,很抱歉我没有正确地看到代码。这是我发现使用mysql函数时首先要查找的;-现在,让我们等待OP的回应,谢谢你能帮我让我的代码只使用mysqli吗?