首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用php脚本将变音插入到mysql数据库中

问使用php脚本将变音插入到mysql数据库中
EN

Stack Overflow用户
提问于 2010-05-20 21:24:22
回答 2查看 3.5K关注 0票数 1

我正在开发一个脚本,它以csv作为输入,然后读取文件并将其内容插入到mysql数据库中。因此,在将数据插入到数据库时出现了问题。它将变音转换为随机字符。

仅供参考--我的数据库在latin_german_ci中。我还尝试将其更改为UTF8

我可以在web浏览器中显示变音字符,但是当我试图通过sql查询将它们插入到数据库中时,它会插入随机字符。

代码语言:javascript
复制
<?php

function uploadCsv($filename){

    echo "filename - ".$filename."<br/>";

    if(isset($filename) || $filename == ""){ // return with an error msg.

    }else{
        $pos = stripos($filename, ".csv");
        if($pos == 0 || $pos != strlen($filename)-4){
            //echo "invalid format";
            return ;
        }
    }

    set_time_limit(0);
    $error = "";
    $row = 0;
    $handle = fopen($filename, "r");
    //echo "<br/>".$filename."<br/>";
    //echo "<br/>".$handle."<br/>";
    if($handle == null){

        return "unable to process";
    }

    while (($data = fgetcsv($handle, 0, ",")) !== FALSE) {

        if ($row == 0) {
            // this is the first line of the csv file
            // it usually contains titles of columns
            // do nothing.
            $num = count($data);
            //echo "num - ".$num."<br/>";
            if($num != 2){
                // echo "returning back";
                return "invalid CSV format.";
            }
        }
        // this handles the rest of the lines of the csv file
        $num = count($data);
        $id = $data[0];
        $inserQuery = "";
        $inserQuery =  "INSERT INTO `table` (
                            `ID` ,
                            `Productname`
                            )
                            VALUES (";

        for ($c=0; $c < $num; $c++) {
            if($c==0){
                $inserQuery .= " '". utf8_encode($data[$c])."'" ;
            }else{
                $inserQuery .= ", '". utf8_encode($data[$c]) ."'" ;
            }
        }
        $inserQuery .= ");";
        echo $inserQuery."<br/>";
        mysql_query($inserQuery);
        if(mysql_affected_rows() == -1 || mysql_affected_rows() <1){
            echo "error<br/>";
        }else{
            echo "row inserted - ".$row." with ID = ".$id." <br/>";
        }
        $row++;
    }
    fclose($handle);
    return "1";
}
?>

请帮帮忙..。

谢谢

EN

回答 2

Stack Overflow用户

发布于 2010-05-20 21:29:41

同样的程序。请参阅

Whether to use "SET NAMES"

和

Is "SET CHARACTER SET utf8" necessary?

从本质上讲,您必须告诉MySQL它应该从客户端(您的PHP脚本)获得哪个字符集。

票数 2
EN

Stack Overflow用户

发布于 2010-05-20 21:29:57

为什么对拉丁语数据库使用utf8_encode()?你的网页的编码是什么(HTTP Content-Type和HTML meta标签)?源代码的编码是什么(你可以在你的编辑器中配置)?您使用哪个程序来查看数据库?使用哪种编码?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2874101

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档