0

I have to build a script to import a CSV file, (With Arabic content in one coloumn).

I have been searching online and manage to convert my CSV file to UTF8

$filename = $_FILES["csv"]["tmp_name"];

$file = fopen($filename, "r");

$count = 0;
while (($emapData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$count++;
if($count>1){

// tried to convert using below
$string_decoded =  iconv("windows-1256", "utf-8", $emapData[2]);
// convert code above

$sql = "INSERT into `$table` (SurahNo,AyatNo,Ayat) values ('$emapData[0]','$emapData[1]','$string_decoded')";
echo $sql;
mysql_query($sql);  
}
}

I tried to convert using iconv but still getting garbage content imported in mysql.

Imported content look like this ط¨ظگط³ظ’ظ…ظگ ط§ظ„ظ„ظ‘ظ

Can share my CSV file if you wish.

Need help, spend hours searching.

Zee
  • 351
  • 1
  • 15
  • Your mysql table supports utf8? – Karl Adler Nov 02 '14 at 13:13
  • yes, this is my database look like CREATE TABLE IF NOT EXISTS `halar_table` ( `SurahNo` int(11) NOT NULL, `AyatNo` int(11) NOT NULL, `Ayat` varchar(1000) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; – Zee Nov 02 '14 at 13:15
  • is the echoed sql displayed correctly or does error appears there too? – Karl Adler Nov 02 '14 at 13:39
  • I thik you need to set your connection charset. I would recommend also to use mysqli or PDO for your database operations. see: http://php.net/manual/en/function.mysql-set-charset.php and http://stackoverflow.com/questions/16893035/using-utf8mb4-with-php-and-mysql – Karl Adler Nov 02 '14 at 13:45
  • echo showing error too. that's what display when echo INSERT into `halar_table` (SurahNo,AyatNo,Ayat) values ('1','1','ط¨ظگط³ظ’ظ…ظگ ط§ظ„ظ„ظ‘ظژظ‡ظگ ط§ظ„ط±ظ‘ظژط­ظ’ظ…ظژظ°ظ†ظگ ط§ظ„ط±ظ‘ظژط­ظگظٹظ…ظگ') – Zee Nov 02 '14 at 13:56
  • So it's seems to be a problem with fgets() function. Could you provide the file or an extract with few lines? – Karl Adler Nov 02 '14 at 15:00
  • Sorry just notice you came back to me, you need line of my import file or csv file? – Zee Nov 03 '14 at 14:03

0 Answers0