Cannot get the correct utf-8 text from Access

前端 未结 1 428
猫巷女王i
猫巷女王i 2021-01-28 05:54

When I tried to get chinese characters from the database, I got weird text. I tried almost everything, like html_entity_decode, htmlentities, save the

相关标签:
1条回答
  • 2021-01-28 06:11

    I encountered this issue a while ago and the only way I could get it to work was to write the HTML into an ADODB.Stream object, save it to a file, and then echo the file:

    <?php
    define("TEMP_FOLDER", "C:\\__tmp\\");
    header('Content-Type: text/html; charset=utf-8');
    
    $stm = new COM("ADODB.Stream") or die("Cannot create COM object.");
    $stm->Type = 2;  // adTypeText
    $stm->Charset = 'utf-8';
    $stm->Open();
    $stm->WriteText('<html>');
    $stm->WriteText('<head>');
    $stm->WriteText('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />');
    $stm->WriteText('<title>ADODB test</title>');
    $stm->WriteText('</head>');
    $stm->WriteText('<body>');
    
    $con = new COM("ADODB.Connection"); 
    $con->Open(
            "Driver={Microsoft Access Driver (*.mdb, *.accdb)};" .
            "Dbq=C:\\Users\\Public\\Database1.accdb");
    $rst = $con->Execute("SELECT word FROM vocab WHERE ID=3");
    $stm->WriteText($rst->Fields("word"));
    $rst->Close();
    $con->Close();
    
    $stm->WriteText('</body>');
    $stm->WriteText('</html>');
    
    $tempFile = TEMP_FOLDER . uniqid("", TRUE) . ".txt";
    $stm->SaveToFile($tempFile, 2);  // adSaveCreateOverWrite
    $stm->Close();
    echo file_get_contents($tempFile);
    unlink($tempFile);
    ?>
    
    0 讨论(0)
提交回复
热议问题