如何从.txt中获取这个城市,并使用PHP将其添加到数据库中


How to get this city from .txt and add this to database using PHP?

city.txt

City
City1
City2
City3
City4
City One
City Other

如何从.txt中获取这个城市,并使用PHP将其添加到数据库中?

或者尝试:

$file = file_get_contents("city.txt");
$cities = explode("'n", $file);
foreach ($cities as $city) {
    $SQL = "INSERT INTO table_name (city) VALUES ('$city')";
    //now execute the SQL statement
}

这很容易。您需要谷歌在PHP中读取文件,并在PHP中写入数据库。

在这里,我将开始你:

$file_handle = fopen("cit.txt", "rb");
while (!feof($file_handle) ) {
  $line_of_text = fgets($file_handle);
  // write your insert statement here, depending on your table structure.
}