简体   繁体   English

通过php和vba将汉字插入mysql

[英]insert chinese character into mysql through php and vba

I tried still cannot input the Chinese character into the field.. 我尝试仍然无法在该字段中输入汉字。

The url looks like this: http://xxx.xxx/AddCategory.php?CatID=0001&NameTC =石油&NameEN=Petroleum%20Gases&Random=1.401764E-02 网址看起来像这样: http ://xxx.xxx/AddCategory.php?CatID=0001&NameTC =石油&NameEN =石油%20Gases&Random = 1.401764E-02

It works for URL but I am using VBA to run the url through php to update the mysql, it doesnt work. 它适用于URL,但我正在使用VBA通过php运行url来更新mysql,但它不起作用。 so maybe due to encoding problem in vba or php? 所以可能是由于VBA或php中的编码问题? Please advise! 请指教!

IN vba, I am using MSXLM2.XMLHTTP 在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 | CatID | CatName_TC | CatName_TC | CatName_EN 猫名_EN

Expected: 预期:

1001 | 1001 | 工業 | 工业| Industrial 产业

Results: 结果:

1001 | 1001 | [Nothing] | [没事] Industrial 产业

I use Excel VBA to store these data into 3 variable and use web of php with parameter to insert into my mysql. 我使用Excel VBA将这些数据存储到3个变量中,并使用带有参数的php web将其插入到我的mysql中。 I could not insert the Chinese character into the database. 我无法将中文字符插入数据库。 I could do it for CatID and CatName_EN, but add nothing on the field CatName_TC. 我可以为CatID和CatName_EN做到这一点,但在字段CatName_TC上什么也不添加。

VBA Code: 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 Code: 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();

The table could have been created with the default latin1 charset instead of utf8 . 该表可能是使用默认的latin1字符集而不是utf8 While it is not possible to convert existing values in CatNameTC column to utf8, you can alter the table for new inserts to use utf8 charset 虽然无法将CatNameTC列中的现有值转换为utf8,但是您可以更改表以供新插入使用utf8字符集

ALTER TABLE AACategory CONVERT TO CHARACTER SET utf8;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM