MySql + php将特殊字符插入db(utf8)

时间:2014-09-09 01:12:07

标签: php mysql special-characters steam-web-api

我有这个代码将Steam Web Api中的数据抓取到变量中,它打印数据然后将其插入数据库。我的问题是,名称(来自网络api的“personaname”)可以有特殊字符,如俄语字符,希腊语等。我用utf8配置php,所以当我直接从api打印到我的这些字符时我没有问题PHP代码,但当我尝试将它们插入数据库时​​,它们被破坏成“????? ?????”或“?? o ?? f ???”。我使用utf8 utf8_unicode_ci配置PDO,并为mysql配置相同的PDO。我尝试使用htmlentities(),htmlspecialchars()和utf8_encode()没有运气。抱歉我的英语不好。

$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);
            }
        }

来自PDO的“connectdb.php”的代码

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;
}

0 个答案:

没有答案