duanou2526 2014-01-26 14:17
浏览 69
已采纳

如何将文本文件插入数据库表mysql

I have a batchcode.txt which contains list of batch code.

645863
302422
430307
821773
599738
671768
732159

and so on

I have a table in my database which is called batchcode with fields 'id','batchcode'.

My problem is it wont insert my text file into my table called batchcode. I already did the import and select table but nothing works...

I need some help can anyone help me please.

<?php
$host= "localhost";
$user= "root";
$pass= "";
$db="klayton";

$connect= mysql_connect($host,$user,$pass);
if (!$connect)die ("Cannot connect!");
mysql_select_db($db, $connect);

$file = fopen("batchcode.txt","r");  

while(! feof($file))
{
$sql = "INSERT INTO batchcode( batchcode ) VALUES ('fgets($file)')"; //Insert every read line from txt to mysql database
mysql_query($sql);
}
fclose($file);
?>

I got an error that says "Fatal error: Maximum execution time of 30 seconds exceeded in C:\xampp\htdocs\test\index.php on line 22"

  • 写回答

2条回答 默认 最新

  • duandu8202 2014-01-26 14:48
    关注

    With your current code the function fgets($file) will never be executed, so ! feof($file) will always true, and the while loop keeps running until execution time limitation.

    You should rewrite the insert query as following:

    while( $batchcode = fgets($file) )
    {
      $sql = "INSERT INTO batchcode( batchcode ) VALUES ('$batchcode')"; 
      mysql_query($sql);
    }    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 html5+css有人可以帮吗?
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?