MySQL PHP加载数据infile

时间:2015-08-03 17:58:15

标签: php mysql

我正在尝试使用PHP更新MySQL数据库,其中要插入的数据来自文本文件。但是,当我从PHP运行查询时,数据库不会更新。

如果我从终端mysql -u root -p --local-infile videos运行mysql客户端并输入LOAD DATA LOCAL INFILE /home/user/movies.txt INTO TABLE films;,它可以正常工作。

以下是我的Index PHP文件中的代码(在Apache2上托管)。

我做错了吗?像语法等?

<?php

$username="root"; $password="12345"; $database="videos";

$con = mysqli_connect("localhost",$username,$password, $database);

mysqli_query("LOAD DATA LOCAL INFILE '/home/user/movies.txt' INTO TABLE films");

$result = mysqli_query($con, "SELECT title, yr, genre, plot, rating FROM films") or die("Unable to query.");

echo "<table border='1'>";
echo "<tr> <th>Title</th> <th>Year</th> <th>Genre</th> <th>Plot</th> <th>Rating</th> </tr>";

// keep getting the next row until no rows left
while($row = mysqli_fetch_array( $result )) {
  // print out contents of each row into a table
  echo "<tr> <td>";
  echo $row['title'];
  echo "</td><td>";
  echo $row['yr'];
  echo "</td><td>";
  echo $row['genre'];
  echo "</td><td>";
  echo $row['plot'];
  echo "</td><td>";
  echo $row['rating'];
  echo "</td></tr>";
}

echo "</table>";
?>

感谢。

编辑: 回复了LOAD DATA LOCAL ..,Errormessage: The used command is not allowed with this MySQL version

的错误消息

1 个答案:

答案 0 :(得分:1)

找到了这个:using mysql LOAD statment in PHP fails, but doing it via command line works

我用过:

$conn = mysqli_init();
mysqli_options($conn, MYSQLI_OPT_LOCAL_INFILE, true);
mysqli_real_connect($conn,server,user,code,database);