gpt4 book ai didi

php - 无法使用 php 和 mysql 将包含 unicode 数据的单个 csv 文件导入到多个表

转载 作者:行者123 更新时间:2023-11-29 07:18:44 25 4
gpt4 key购买 nike

我正在尝试使用 php 和 mysql 将包含 unicode 数据的单个 csv 文件导入到多个表中。我能够将数据插入到多个表中,但不支持 unicode。我有带有尼泊尔数据的 csv 文件。请帮我。提前致谢 enter image description here

<?php
header('Content-Type: charset=utf-8');
require_once('conn.php');
if(isset($_POST["submit"])){

$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
// echo $file;die;
$c = 0;
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
// while(($filesop = fgetcsv($handle, 1000, "\t")) !== false)
{
$filesop = array_map("utf8_encode", $filesop);
// var_dump($filesop);die;
$id = $filesop[0];
// echo $id;die;
$registration_date = $filesop[1];

$building_use_id = $filesop[2];
$building_category = $filesop[3];


$a=utf8_encode($registration_date);

$bps_registration_newregistration = "insert into bps_registration_newregistration(id,registration_date) values ('$id','$registration_date')";
$stmt = mysqli_prepare($conn,$bps_registration_newregistration);
mysqli_stmt_execute($stmt);
$bps_registration_application = "insert into bps_registration_application(reg_id,registration_date,building_use_id,building_category) values ('$id','$a','$building_use_id','$building_category')";
$stmt1 = mysqli_prepare($conn,$bps_registration_application);
mysqli_stmt_execute($stmt1);


$c = $c + 1;
}

if($bps_registration_newregistration && $bps_registration_application ){
echo "sucess";
}

else
{
echo "Sorry! Unable to impo.";
}
}
?>
<!DOCTYPE html>
<html>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<body>
<form enctype="multipart/form-data" method="post" role="form" accept-charset="UTF-8">
<div class="form-group">
<label for="exampleInputFile">File Upload</label>
<input type="file" name="file" id="file" size="150">
<p class="help-block">Only Excel/CSV File Import.</p>
</div>
<button type="submit" class="btn btn-default" name="submit" value="submit">Upload</button>
</form>
</body>
</html>


最佳答案

您的文件是制表符分隔的,而不是逗号分隔的,因此您需要告诉函数:

while(($filesop = fgetcsv($handle, 1000, "\t")) !== false)

关于php - 无法使用 php 和 mysql 将包含 unicode 数据的单个 csv 文件导入到多个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57678419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com