What is the equivalent PHP command for loading data from a text file into a table, similar to the MySQL command mentioned in the thread?
To load data from a text file into a table in PHP, you can use the `LOAD DATA INFILE` MySQL command. This command allows you to directly import data from a text file into a MySQL table. However, since this command is specific to MySQL, you cannot directly use it in PHP. Instead, you can read the contents of the text file in PHP and then insert the data into the MySQL table using the appropriate SQL queries.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Read data from text file
$file = fopen("data.txt", "r");
while (!feof($file)) {
$data = fgets($file);
// Insert data into MySQL table
$sql = "INSERT INTO table_name (column1, column2, column3) VALUES ($data)";
$conn->query($sql);
}
fclose($file);
$conn->close();
?>