UTF8在PHP的MySQL(UTF8 in php mysql)

编程入门 行业动态 更新时间:2024-10-19 19:43:48
UTF8在PHP的MySQL(UTF8 in php mysql)

我试图通过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 } }

更多推荐

本文发布于:2023-08-01 03:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1353963.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PHP   MySQL   mysql   php

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!