我在数据库中有一个表:
Time Name
11:23 Tom
12:22 Jack
和.txt文件
12:22, Anna
这是我的代码
$connection = mysqli_connect($hostname, $username, $password, $dbname) or die("Error " . mysqli_error($connection));
// open txt file
$fp = fopen($filename,"r");
//parse the csv file row by row
while(($row = fgetcsv($fp,"500",",")) != FALSE)
{
//insert csv data into mysql table
$sql = "INSERT INTO project-1 (time, name) VALUES('" . implode("','",$row) . "')";
if(!mysqli_query($connection, $sql))
{
die('Error: ' . mysqli_error($connection));
}
}
fclose($fp);
//close the db connection
mysqli_close($connection);
如果数据库中的时间值= .txt中的时间值
,我想停止将.txt文件导入数据库答案 0 :(得分:0)
我不知道我是否正确理解你,但我建议你这样做。如果你有其他问题,请告诉我,我会帮助你。
$conn=mysqli_connect('host','username','password','dbname');
$file=fopen('a.txt','r');
if($file){
while(($line=fgets($file))!==null){
//read every line in txt and but before inserting into db check the time like this:
//first parse the time out of the line:
$time_array=explode(',',$line);
//now you have an array with to indexes. 0=time, 1=name.
$query="SELECT * FROM table WHERE time='$time_array[0]'";
$result=mysqli_query($conn,$query);
if(mysqli_num_rows($result)==0){
//code for inserting into db;
}else{
//stop inserting code and do fclose() or anything else.
}
}
}