2013-07-15 3 views
-2

PHP 스크립트를 사용하여 텍스트 파일에서 데이터베이스 테이블에 항목을 추가하려고합니다. 텍스트 파일에는 2000 개의 행이 들어 있으며 각 행을 데이터베이스 테이블의 한 항목으로 지정합니다. 이것은 내 코드입니다. 그것은 잘 실행되지만 아무것도 테이블에 추가됩니다.파일에서 데이터베이스 테이블에 항목 추가

<?php 

$link = new mysqli('xxx.xxx.xxx.xx', 'xxxxxx', 'xxxxxxxx', 'xxxxxxxxxxx'); 
if ($link->connect_errno) { 
    die('Failed to connect to MySQL: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); 
} 
$filename = 'serials.txt'; 
$file = file($filename); 
foreach($file as $line) { 
    $result = $link->query("INSERT INTO mytable SET serial=$line"); 
} 
?> 
+0

왜 LOAD 데이터를 사용할 수 있습니까? – Strawberry

+0

나는 그것에 대해 들어 본 적이 없으며 그것을 사용하는 방법을 모른다. –

+0

그래서 지금은 배울 좋은 시간이 될 것이라고 생각하지 않습니까? – Strawberry

답변

2
  1. 인쇄 에러 메시지
  2. escape 데이터
<?php 

$link = new mysqli('xxx.xxx.xxx.xx', 'xxxxxx', 'xxxxxxxx', 'xxxxxxxxxxx'); 
if ($link->connect_errno) { 
    die('Failed to connect to MySQL: (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error); 
} 
$filename = 'serials.txt'; 
$file = file($filename); 
foreach($file as $line) { 
    $result = $link->query("INSERT INTO mytable SET serial='" 
      .$link->real_escape_string($line)."'"); 
    if($link->errno) echo($link->errno.": ".$link->error); 
} 
?> 
관련 문제