2017-03-04 6 views
0

PHPを使用してmysql dbに特殊文字(エンティティ)を挿入しようとしています。PHPを使用して特殊文字int mysqldbを挿入する方法

<?php 
    $servername = "localhost"; 
    $username = "root"; 
    $password = ""; 
    $dbname = "test"; 

$autoid = $_REQUEST["autoid"]; 
// $explanation = htmlspecialchars($_REQUEST["explanation"]); 
$explanation = mysql_real_escape_string("<p>hello how are you Ê</p>"); 



    echo $explanation; 

    // Create connection 
    $conn = new mysqli($servername, $username, $password, $dbname); 

    //$explanation = utf8_decode(trim(mysql_real_escape_string($explanation)); 
    // Check connection 
    if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
    } 

    $sql = "UPDATE question_master SET explanation='$explanation' WHERE autoid=$autoid"; 

    if ($conn->query($sql) === TRUE) { 
     echo "Record updated successfully"; 
    } else { 
     echo "Error updating record: " . $conn->error; 
    } 

    $conn->close(); 
?> 

私が合格しようとする文字列が<p>hello how are you Ê</p>ですが、MySQLdbはで更新した後、それが<p>hello how are you Ê</p>となります。私はmysqlに新しいです、何がうまくいかないか考えません。テーブルの照合順序があるutf8mb4_bin

表の照合順序は、ある utf8_bin

+0

http://stackoverflow.com/questions/8568301/how-to-insert-special-language-charactersだから、行くための適切な方法は次のようなものになるだろう-in-php-into-mysql-database – ashanrupasinghe

+0

ADD "" –

答えて

0

このようmysql_real_escape_string()を呼び出す前に、期待通りにエスケープ仕事をするために文字セットを設定してみてください:

mysql_set_charset("utf8mb4"); 

$explanation = mysql_real_escape_string("<p>hello how are you Ê</p>"); 

編集:私はちょうど指摘減量したmysql_real_escape_string()を使用しています。代わりにmysqli_real_escape_string()を使用してください。より具体的には、リンク識別子をMySQL接続に渡すことで、特定の接続の文字セットを設定することをお勧めします。

$conn = new mysqli($servername, $username, $password, $dbname); 

mysqli_set_charset($conn, "utf8mb4"); 

$explanation = mysqli_real_escape_string($conn, "<p>hello how are you Ê</p>"); 

ドキュメント: http://php.net/manual/en/mysqli.set-charset.php http://php.net/manual/en/mysqli.real-escape-string.php

関連する問題