开发者

Inserting UMLAUT into mysql database using a php script

i am developing a script which takes a csv as an input, it then reads the file and insert its contents to a mysql database. So the problem comes while inserting the data to the database. It converts UMLAUT into random characters.

FYI -- My database is in latin_german_ci. [ i have also tried changing i开发者_StackOverflow中文版t to UTF8]

i am able to display UMLAUT characters in web browsers but when i try to insert them in a database through a sql query, it inserts random characters.

<?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";
}
?>

Please help....

thanks


Same procedure... Please see

Whether to use "SET NAMES"

and

Is "SET CHARACTER SET utf8" necessary?

Essentially the thing is that you have to tell MySQL which character set it should expect from the client (your PHP script).


Why do you use utf8_encode() for a latin database? Whats the encoding of your webpage (HTTP Content-Type and HTML meta tags)? Whats the encoding of the source code (you can configure this in your editor)? Which program do you use to view the database and which encoding does this use?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜