我试图通过PHP从xls导入数据到MySQL。 我在通过它保存UTF-8文本时遇到了问题。 我将它保存为???????。 我的数据库表结构是utf8_general_ci以及我的php代码如下所示
<?php $con = mysqli_connect('localhost', 'myuser', 'mypass', 'mydb'); if(isset($_POST["submit"])) { mysqli_query($con,'SET character_set_results=utf8'); mysqli_query($con,'SET names=utf8'); mysqli_query($con,'SET character_set_client=utf8'); mysqli_query($con,'SET character_set_connection=utf8'); mysqli_query($con,'SET character_set_results=utf8'); mysqli_query($con,'SET collation_connection=utf8_general_ci'); $file = $_FILES['file']['tmp_name']; $handle = fopen($file, "r"); $i = 0; while(($filesop = fgetcsv($handle, 1000, ",")) !== false) { $option1 = $filesop[0]; $option2 = $filesop[1]; $option3 = $filesop[2]; $option4 = $filesop[3]; $correctans = $filesop[4]; $question_text = $filesop[5]; $cat_id = $filesop[6]; $sub_cat_id = $filesop[7]; $level_id = $filesop[8]; $quesimage = $filesop[9]; $sql = mysqli_query($con,"INSERT IGNORE INTO questions (option1, option2,option3,option4,correctans,question_text,cat_id,sub_cat_id,level_id,quesimage) VALUES ('".$option1."','".$option2."','".$option3."','".$option4."','".$correctans."','".$question_text."','".$cat_id."','".$sub_cat_id."','".$level_id."','".$quesimage."')"); $i = $i + 1; } //echo $sql; if($sql) { echo "You database has imported successfully. You have inserted ". $i ." records"; } else { echo "Sorry!"; } } ?> <html> <head> <title>Import Questions</title> </head> <body> <form method="post" action="" enctype="multipart/form-data"> Upload Excel File : <input type="file" name="file" /><br /> <input type="submit" name="submit" value="Upload" /> </form> </body> </html>它的英文文本工作正常,但在印地文或古吉拉特语文本中出现问题。 我该如何解决它?
谢谢
I am trying to import data from xls to mysql via php. I am facing issue in save UTF-8 text via it. I am getting it saved as ???????. My database table structure is utf8_general_ci as well my php code is like below
<?php $con = mysqli_connect('localhost', 'myuser', 'mypass', 'mydb'); if(isset($_POST["submit"])) { mysqli_query($con,'SET character_set_results=utf8'); mysqli_query($con,'SET names=utf8'); mysqli_query($con,'SET character_set_client=utf8'); mysqli_query($con,'SET character_set_connection=utf8'); mysqli_query($con,'SET character_set_results=utf8'); mysqli_query($con,'SET collation_connection=utf8_general_ci'); $file = $_FILES['file']['tmp_name']; $handle = fopen($file, "r"); $i = 0; while(($filesop = fgetcsv($handle, 1000, ",")) !== false) { $option1 = $filesop[0]; $option2 = $filesop[1]; $option3 = $filesop[2]; $option4 = $filesop[3]; $correctans = $filesop[4]; $question_text = $filesop[5]; $cat_id = $filesop[6]; $sub_cat_id = $filesop[7]; $level_id = $filesop[8]; $quesimage = $filesop[9]; $sql = mysqli_query($con,"INSERT IGNORE INTO questions (option1, option2,option3,option4,correctans,question_text,cat_id,sub_cat_id,level_id,quesimage) VALUES ('".$option1."','".$option2."','".$option3."','".$option4."','".$correctans."','".$question_text."','".$cat_id."','".$sub_cat_id."','".$level_id."','".$quesimage."')"); $i = $i + 1; } //echo $sql; if($sql) { echo "You database has imported successfully. You have inserted ". $i ." records"; } else { echo "Sorry!"; } } ?> <html> <head> <title>Import Questions</title> </head> <body> <form method="post" action="" enctype="multipart/form-data"> Upload Excel File : <input type="file" name="file" /><br /> <input type="submit" name="submit" value="Upload" /> </form> </body> </html>its working fine with English Text but getting issue in Hindi or Gujarati Text. How can I solve it ?
Thanks
最满意答案
请注意,使用fgetcsv()函数读取数据时,会考虑locale设置。 如果LANG是例如en_US.UTF-8 ,则使用此函数读取单字节编码的文件。
您可以尝试另一件事 - 即时转换您的.csv文档(使用您的文件编码更改UCS-2 ):
function parse_csv($filename) { if (($handle != fopen($filename, "r"))) return false; while (($cols = fgetcsv($handle, 1000, "\t")) !== FALSE) { foreach( $cols as $key => $value ) { $cols[$key] = trim( $cols[$key] ); $cols[$key] = iconv('UCS-2', 'UTF-8', $cols[$key]."\0") ; $cols[$key] = str_replace('""', '"', $cols[$key]); $cols[$key] = preg_replace("/^\"(.*)\"$/sim", "$1", $cols[$key]); } echo var_dump($cols); //This will display an array of your data } }Note that when using fgetcsv() function for reading data the locale setting is taken into account. If LANG is e.g. en_US.UTF-8, files in one-byte encoding are read wrong by this function.
You can try another thing - to convert your .csv document on-the-fly (change the UCS-2 with the your file encoding):
function parse_csv($filename) { if (($handle != fopen($filename, "r"))) return false; while (($cols = fgetcsv($handle, 1000, "\t")) !== FALSE) { foreach( $cols as $key => $value ) { $cols[$key] = trim( $cols[$key] ); $cols[$key] = iconv('UCS-2', 'UTF-8', $cols[$key]."\0") ; $cols[$key] = str_replace('""', '"', $cols[$key]); $cols[$key] = preg_replace("/^\"(.*)\"$/sim", "$1", $cols[$key]); } echo var_dump($cols); //This will display an array of your data } }
更多推荐
发布评论