You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.5 KiB
65 lines
1.5 KiB
<?php
|
|
$servername = "localhost";
|
|
$username = "root";
|
|
$password = "root";
|
|
$dbname = "tracking_2d";
|
|
$tableName = "records";
|
|
$dataFileName = "../data/results2.txt";
|
|
|
|
// Create connection
|
|
$conn = new mysqli($servername, $username, $password, $dbname);
|
|
// Check connection
|
|
if ($conn->connect_error) {
|
|
die("Connection failed: " . $conn->connect_error);
|
|
}
|
|
|
|
|
|
|
|
|
|
// $fn = fopen("myfile.txt", "r");
|
|
// $result = fgets($fn);
|
|
// echo $result;
|
|
// fclose($fn);
|
|
|
|
$handle = fopen($dataFileName, "r");
|
|
if ($handle) {
|
|
$text="";
|
|
$counter = 0;
|
|
while (($line = fgets($handle)) !== false) {
|
|
// process the line read.
|
|
if (substr($line, 0, 1) === '[') { //Remove the first '['
|
|
$line = substr($line, 1);
|
|
}
|
|
if (substr($line, -1, 1) === ']') { //Remove the last ']'
|
|
$line = substr($line, 0, -1);
|
|
}
|
|
if (substr($line, -3, 1) === ',') { //Remove the ',\n'
|
|
$line = substr($line, 0, -3);
|
|
}
|
|
|
|
//insert record in database
|
|
$sql = "INSERT INTO " . $tableName . " (data, test_name) VALUES ('".$line."', 'TEST_1')";
|
|
if ($conn->query($sql) === TRUE) {
|
|
$text = $text."Line ".$counter."added\n";
|
|
} else {
|
|
$text = $text . "Line " . $counter . "NOT added\n";
|
|
}
|
|
$counter++;
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
echo $text;
|
|
} else {
|
|
echo "Error opening file";
|
|
}
|
|
|
|
|
|
|
|
// if ($conn->query($sql) === TRUE) {
|
|
// echo "New record created successfully";
|
|
// } else {
|
|
// echo "Error: " . $sql . "<br>" . $conn->error;
|
|
// }
|
|
|
|
$conn->close();
|