MySql+php在数据库中插入特殊字符(utf8)
我得到了这段代码,它从Steam Web Api中获取数据,并将数据打印出来,然后插入数据库。我的问题是web api中的名称personaname可能有特殊字符,如俄语字符、希腊语字符等。我用utf8配置了php,所以当我直接从api打印到php代码时,这些字符没有问题,但当我尝试将它们插入数据库时,它们被损坏了?????或者说是o?f???。我用utf8 utf8_unicode_ci配置了PDO,mysql也是如此。我尝试使用htmlspecialchars和utf8_编码,但没有成功。对不起,我的英语不好MySql+php在数据库中插入特殊字符(utf8),php,mysql,special-characters,steam-web-api,Php,Mysql,Special Characters,Steam Web Api,我得到了这段代码,它从Steam Web Api中获取数据,并将数据打印出来,然后插入数据库。我的问题是web api中的名称personaname可能有特殊字符,如俄语字符、希腊语字符等。我用utf8配置了php,所以当我直接从api打印到php代码时,这些字符没有问题,但当我尝试将它们插入数据库时,它们被损坏了?????或者说是o?f???。我用utf8 utf8_unicode_ci配置了PDO,mysql也是如此。我尝试使用htmlspecialchars和utf8_编码,但没有成功。对
$steamkey = "x";
$id_banuser_final = "76561198088987175";
$api = "http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/? key=".$steamkey."&steamids=".$id_banuser_final;
// Initiate curl
$ch1 = curl_init();
// Disable SSL verification
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
// Will return the response, if false it print the response
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
// Set the url
curl_setopt($ch1, CURLOPT_URL,$api);
// Execute
$json=curl_exec($ch1);
// Closing
curl_close($ch1);
$decoded = json_decode($json);
$nombre = $decoded->response->players[0]->personaname;
print_r($nombre);
echo "<br/>";
if(isset($decoded->response->players[0]->loccountrycode)) {
$loccountrycode = $decoded->response->players[0]->loccountrycode;
print_r($loccountrycode);
echo "<br/>";
}
$steam_id_sixtyfour = $decoded->response->players[0]->steamid;
print_r($steam_id_sixtyfour);
echo "<br/>";
$avatarfull = $decoded->response->players[0]->avatarfull;
echo "<img src='".$avatarfull."' alt='Avatarfull' />";
echo "<br/>";
$avatarmedium = $decoded->response->players[0]->avatarmedium;
echo "<img src='".$avatarmedium."' alt='Avatarmed' />";
echo "<br/>";
$avatar = $decoded->response->players[0]->avatar;
echo "<img src='".$avatar."' alt='Avatar' />";
echo "<br/>";
$profileurl = $decoded->response->players[0]->profileurl;
print_r($profileurl);
echo "<br/>";
$datenow = date("Y-m-d");
echo $datenow;
echo "<br/>";
echo "............................................";
echo "<br/>";
require_once('incluidos/connectdb.php');
$db = connect_db();
$q ="INSERT INTO `d2bd`.`profiles`(`steamid`,`fecha_cons`,`loccountrycode`,`personaname`,`avatar`,`avatarmedium`,`avatarfull`,`profileurl`) VALUES(?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE fecha_cons=?, loccountrycode=?, personaname=?, avatar=?, avatarmedium=?, avatarfull=?";
$nombrebind = utf8_encode($nombre);
$statement = $db->prepare($q);
$statement->bindParam(1, $steam_id_sixtyfour);
$statement->bindParam(2, $datenow);
$statement->bindParam(3, $loccountrycode);
$statement->bindParam(4, $nombrebind);
$statement->bindParam(5, $avatar);
$statement->bindParam(6, $avatarmedium);
$statement->bindParam(7, $avatarfull);
$statement->bindParam(8, $profileurl);
$statement->bindParam(9, $datenow);
$statement->bindParam(10, $loccountrycode);
$statement->bindParam(11, $nombrebind);
$statement->bindParam(12, $avatar);
$statement->bindParam(13, $avatarmedium);
$statement->bindParam(14, $avatarfull);
$statement->execute();
echo "-.-.-.--.-.-.-.-..--.-.-";
echo "<br/>";
$q2 ="SELECT * FROM profiles WHERE steamid = ?";
$statement2 = $db->prepare($q2);
$statement2->bindParam(1, $steam_id_sixtyfour);
$status = $statement2->execute();
if(($status) && ($statement2->rowCount() > 0)) {
while($row=$statement2->fetch()){
$fecha_dato=$row['personaname'];
$fecha2= utf8_decode($fecha_dato);
print_r($fecha2);
}
}
如何查看损坏的数据库条目?此外,如果您的connect_db函数无法连接,那么它应该退出。我检查了SQLyog中的条目,然后在php中添加了一个SELECT,用于比较API中的personame和数据库中的personame。关于连接数据库你是说我需要在捕获中添加一个中断?太酷了。我只是问,无论您使用什么来查看条目,都需要与UTF兼容,但我可以看到,您已经通过直接从API显示结果,然后通过SELECT来完成这项工作。关于你的函数,我不会捕捉到异常。在插入db之前,如何将变量转换为utf8:你的profiles表是什么字符集/排序规则?它的专栏呢?这可能值得一读~
function connect_db()
{
try
{
$connection = new PDO('mysql:host=localhost;dbname=d2bd;charset=utf8', 'root', '123123');
$connection->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$connection->setAttribute(PDO::ATTR_PERSISTENT, true);
$connection->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'utf8' COLLATE 'utf8_unicode_ci'");
}
catch (PDOException $e)
{
// Proccess error
echo 'Cannot connect to database: ' . $e->getMessage();
}
return $connection;
}