I am pretty new to both php and mysql. I have a txt file with a table on it that has each column seperated by a semi-colon column1;column2 and each row is on a new line in the txt file.
column1-row1;column2-row1
column1-row2;column2-row2
I am trying to upload the txt file to a mysql table using phpmyadmin. The first row goes in just fine, but the second one doesn’t. I have tried \r and \n and also \r\n but none of these are working. Those only drop the second result on the txt file to another line (not row) on the first mysql row. So everything is going into the first row on the mysql table.
I have also tried putting a semi-colon after the last column as well column1;column2; but all this does is stop after the fist row.
I guess I am asking how I format the txt file to make each line on the txt file start a new row in the sql table. Or maybe I need to set something up on phpmyadmin first. This is probably extremely easy, but again, I am very new to both php and sql. Thanks so much.
Okay, It’s the same issue I originally had. I have a database, and in that database I have a table with two columns ‘a’ and ‘b’. I also have a txt file I created already that has 50 rows of data. On the txt file, I have seperated ‘a’ and ‘b’ with a semicolon a;b and then I have hit return on my keyboard to make the next row of data. When I upload the txt file using either SQL or your PHP script and then browse the table on my database, it has only entered the first row from my txt file and after the ‘b’ in the first row (on my database table), it has started to enter the second row of data (from my txt file) on a new line but still the same row. I would like the second from my txt file to be placed in a new row on my database table. Maybe I can’t, but that’s the problem I am having.
This is the code I am using:
<?php
$user_name = "username";
$password = "password";
$database = "database";
$server = "server";
$db = mysql_connect($server, $username, $password);
if (!$db) {
die('Could not connect: ' . mysql_error());
}
$fileHandle = fopen('my_text_file.txt', 'rb');
while (!feof($fileHandle)) {
$cols = explode(';', fgets($fileHandle));
$query = sprintf(
'INSERT INTO database_table (field1_name, field2_name) VALUES ("%s", "%s")',
$cols[0], $cols[1]
);
mysql_query($query);
}
fclose($fileHandle);
mysql_close($db);
?>
And my txt file looks like this:
a;b
c;d
e;f
Did you try this?:
And then click the submit (go) button.
EDIT: If you go the PHP route, try this:
It should work assuming each “row” is separated by a newline in the text file.