我试图从我的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();
}
}
?>
请某人帮我这个..谢谢!
答案 0 :(得分:0)
在 php 中添加 utf-8 ,您可以使用:
header('Content-type: text/plain; charset=utf-8');
并且在您使用的代码中:mysql_query
和mysqli_query
为什么会这样,只使用一种格式并更好地使用mysqli_query
即mysqli
个函数。
并且最好像@ Fred-ii-建议的那样进行适当的修正。
答案 1 :(得分:0)
尝试在连接到数据库后运行SET NAMES 'utf8'
和SET CHARSET 'utf8'
。
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARSET 'utf8'");