我已经编码了一个数据库UTF8,因为它包含阿拉伯字母。我有一个php文件从给定表中选择所有值:
<?php header('Content-type: application/json; charset=utf-8');
$host = "localhost"; //Your database host server
$db = "kalimat"; //Your database name
$user = "root"; //Your database user
$pass = ""; //Your password
$connection = mysqli_connect($host, $user, $pass);
//Check to see if we can connect to the server
if(!$connection)
{
die("Database server connection failed.");
die(mysqli_error($db));
}
else
{
//Attempt to select the database
$dbconnect = mysqli_select_db($connection, $db);
//Check to see if we could select the database
if(!$dbconnect)
{
die("Unable to connect to the specified database!");
}
else
{
$auteur=utf8_decode(urldecode($_GET["artist"]));
//$auteur=utf8_decode($auteur);
$resultset = mysqli_query($connection,"SELECT * FROM $auteur");
if( $resultset )
{
$records = array();
//Loop through all our records and add them to our array
while($r = mysqli_fetch_assoc($resultset))
{
$records[] = $r;
}
$records = array_map(function($r) {
$r['artist'] = utf8_encode($r['artist']);
return $r;
}, $records);
//Output the data as JSON
echo json_encode($records);
}
else{
die("Error:".mysqli_error()); //Here is the error
}
}
}
?>
在输出中,我有"Error:
"没有任何规格,所以我不知道问题的来源。php文件中有什么奇怪的地方吗?
谢谢你的帮助。
您试过utf8mb4吗?
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
utf8mb4是utf8的超集
建议:数据库、表名和列应该使用非utf8字符。
编辑:处理utf8字符,你必须使所有在utf8,如:
你的文件,转换为utf8,所有的页面,你正在使用utf8,也就是你得到作者的页面。
数据库、表、列
您编写代码的编辑器
头。
…
尝试将以下行放在PHP标记开始之后,将显示错误。
error_reporting(E_ERROR);
ini_set("display_errors",1);
您正在使用mysqli,而不是mysql。因此,您必须使用适当的错误函数,并根据其正确的语法调用它。
此外,您在用户定义表中存储数据的想法是错误的。你必须先学习关系数据库的基础知识。