im 尝试从我的 android 应用程序将符号添加到数据库中.. 例如,我想将此 A ⊂ B 添加 .. 这将另存为 A ?数据库中的 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-的建议进行适当的更正。
尝试运行SET NAMES 'utf8'
并在连接到数据库后SET CHARSET 'utf8'
。
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARSET 'utf8'");