使用php将gujarati数据从csv存储到MySQL数据库

时间:2015-10-31 19:42:26

标签: php mysql csv

以下代码用于浏览csv文件。 从csv中获取数据并存储在MySQL数据库中。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>

<body>
    <form name="import" method="post" enctype="multipart/form-data">
        કેમ છે: <input type="file" name="file" /><br />
        <input type="submit" name="submit" value="Submit" />
    </form>
<?php
    include ("connection.php");

    if(isset($_POST["submit"]))
    {
        $file = $_FILES['file']['name'];
        $handle = fopen($file, "r");
        $c = 0;
        while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
        {
            $name = $filesop[0];
            $marks = $filesop[1];
            mysql_query("set name utf8");
            $query = "INSERT INTO temp (name, marks) VALUES ('".$name."','".$marks."')";
            echo $query;
            $sql = mysql_query($query);
        }
    }
?>

    </div>
</body>
</html>

以下代码用于连接数据库。

<?php

    $hostname = "localhost";
    $username = "root";
    $password = "";
    $database = "test";
    $conn = mysql_connect("$hostname","$username","$password") or die(mysql_error());
    mysql_select_db("$database", $conn) or die(mysql_error());

?>

当我运行代码时,其存储的数据如“????????”。

我的CSV文件如下

enter image description here

关注phpmyadmin版本信息

enter image description here

以下是我的表结构

enter image description here

1 个答案:

答案 0 :(得分:1)

如果你在数据库中存储 ???????? ,那么,

如果简单echo "rvm install 2.2.3" | /bin/bash -l 无法正常工作,您需要先检查Router.run(routes, Router.HistoryLocation ,将其更改为database collationutf8_general_ci

其次,如果utf8mb4_general_ci没问题,那么字符串也可能已经采用utf_general_ci格式,并且您再次强行转换它。

使用以下函数将字符串转换为collation

utf8

然后在你的代码中

utf8

并查看此处并设置db function convToUtf8($str){ if( mb_detect_encoding($str,"UTF-8, ISO-8859-1, GBK")!="UTF-8" ) return iconv("gbk","utf-8",$str); else return $str; } enter image description here

相关问题