通过php和vba将中文字符插入mysql

时间:2014-10-06 09:28:02

标签: php mysql vba

我试过还是不能将字符输入到字段中..

网址如下所示: http://xxx.xxx/AddCategory.php?CatID=0001&NameTC =石油&安培; NameEN =石油%20Gases&安培;随机= 1.401764E-02

它适用于URL,但我使用VBA通过php运行url来更新mysql,它不起作用。所以也许是因为vba或php中的编码问题?请指教!

IN vba,我正在使用MSXLM2.XMLHTTP

Set oHttp = CreateObject("MSXML2.XMLHTTP")
    If Err.Number <> 0 Then
    Set oHttp = CreateObject("MSXML.XMLHTTPRequest")
        MsgBox "Error 0 has occured while creating a    MSXML.XMLHTTPRequest object"
    End If

CatID | CatName_TC | CatName_EN

预期:

1001 |工业|工业

结果:

1001 | [没什么] |工业

我使用Excel VBA将这些数据存储到3个变量中并使用带有参数的php网页插入到我的mysql中。我无法将中文字符插入数据库。我可以为CatID和CatName_EN执行此操作,但在字段CatName_TC上不添加任何内容。

VBA代码:

    Set oHttp = CreateObject("MSXML2.XMLHTTP")
        If Err.Number <> 0 Then
            Set oHttp = CreateObject("MSXML.XMLHTTPRequest")
            MsgBox "Error 0 has occured while creating a MSXML.XMLHTTPRequest object"
        End If
        On Error GoTo 0
        If oHttp Is Nothing Then
            MsgBox "For some reason I wasn't able to make a MSXML2.XMLHTTP object"
            Exit Function
        End If

        'Open the URL in browser object
        On Error Resume Next
        oHttp.Open "GET", sURL, False
        oHttp.Send
        oHttp.waitforresponse (10)
        GetWebHTML =

 oHttp.responseText

php代码:

    $mysqli = new mysqli("site", "name", "pass", "db");

    $CatID = isset($_GET['CatID']) ? htmlspecialchars($_GET["CatID"]) : NULL;
    $NameTC = isset($_GET['NameTC']) ? htmlspecialchars($_GET["NameTC"]) : NULL;
    $NameEN = isset($_GET['NameEN']) ? htmlspecialchars($_GET["NameEN"]) : NULL;

    $stmt = $mysqli->prepare("INSERT INTO AACategory (AACatID, CatNameTC, CatNameEN) VALUES (?,?,?)");

    // TODO check that $stmt creation succeeded

    // "s" means the database expects a string
    $stmt->bind_param("sss", $CatID, $NameTC, $NameEN);

    $stmt->execute();

    $stmt->close();

    $mysqli->close();

1 个答案:

答案 0 :(得分:0)

该表可能是使用默认的latin1字符集而不是utf8创建的。虽然无法将CatNameTC列中的现有值转换为utf8,但您可以更改新插入的表以使用utf8 charset

ALTER TABLE AACategory CONVERT TO CHARACTER SET utf8;