将数据从文本文件插入到mysql db white php中

时间:2014-10-12 17:02:39

标签: php mysqli

我试过这段代码,但我不工作

    <?php

$con=mysqli_connect("localhost","root","","db_insert_txt");

// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else{
echo "rak connecté";
}
$lines = file('test.txt');  
$test = "";  
$insert_string = "INSERT INTO `test_txt`(`test`) VALUES"; 
$counter = 0; 
$maxsize = count($lines); 
foreach($lines as $line => $company) { 
$insert_string .= "('".$company."')"; 
$counter++; 
if($counter < $maxsize){ 
$insert_string .= ","; 
}//if 
}//foreach 
mysqli_query($insert_string) or die(mysqli_error());
mysqli_close($con);
?>

它给了我以下警告:

  

警告:mysqli_query()期望参数1为mysqli,字符串为

1 个答案:

答案 0 :(得分:1)

错误消息很好地描述了错误。

您需要将DB连接参数传递给查询:

mysqli_query($con, $insert_string);

还可以使用以下内容:

mysqli_error($con);