Hi all
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 it 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