gpt4 book ai didi

php - 将 CSV 插入 MySQL

转载 作者:行者123 更新时间:2023-11-29 06:36:57 25 4
gpt4 key购买 nike

我想将 CSV 插入 MySQL。如果 MySQL 表中存在 sha1 列,如何才能只更新我的表?如果 sha1 列存在,则将其插入。

我有这个代码来插入 csv:

while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}

最佳答案

检查一下,这可能对您有帮助

<?php
while(($csvdata = fgetcsv($handle,10000,","))!== FALSE){
if($i>0) {
$sha1 = $csvdata[0];
$vsdt = $csvdata[1];
$trendx = $csvdata[2];
$notes = $csvdata[3];

// Get record where sha1
$check_sha = "SELECT sha1 FROM jeremy_table_trend WHERE sha1='".$sha1."'";
$check_shaquery = mysqli_query($con , $check_sha);
if($check_shaquery){
$sha_count = mysqli_num_rows($check_shaquery);
}

// Check if sha1 already in database
if(isset($sha_count) && $sha_count>0){
$sql = "UPDATE `jeremy_table_trend` SET `date_sourced`='".$date."',`sha1`='".$sha1."',`vsdt`='".$vsdt."',`trendx`='".$trendx."',`notes`='".$notes."' WHERE sha1='".$sha1."'";
$query = mysqli_query($con , $sql);
}else{
$sql = "INSERT INTO jeremy_table_trend (date_sourced,sha1,vsdt,trendx,notes) VALUES ('$date','$sha1','$vsdt','$trendx','$notes')";
$query = mysqli_query($con , $sql);
}

$c = $c+1;
error_reporting(E_ALL ^ E_NOTICE);
}
$i++;
error_reporting(E_ALL ^ E_NOTICE);
}
?>

关于php - 将 CSV 插入 MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53409552/

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