0

This is the example of my text file:

Name: Chloe
Class: 1A
School: EEE

This is my database:

Name      Class     School

How do I input the text file data into my database using php? Thank you.

2
  • 2
    Since this is homework, I'm obligated by StackOverflow law to ask you what you've tried so far. We're happy to help point out any really obvious mistakes, or nudge you in the right direction, but we won't outright do your homework for you. Commented Aug 24, 2011 at 7:44
  • you can't, the load data require two different LINES TERMINATED character (normally is \n) and FIELD TERMINATED character. In your text file example, you are using new line for both, which is not feasible. The better choice is re-arrange your text file to fulfill the condition. Commented Aug 24, 2011 at 8:47

2 Answers 2

1

This will help read a line in the file into an array:

see: http://www.homeandlearn.co.uk/php/php10p7.html

now once you have the 3 lines, you just need to get the data into your database....

here's some info on the command you will probably need to use to insert/update the data http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

here's some useful info on reading files : http://www.w3schools.com/PHP/php_file.asp

Sign up to request clarification or add additional context in comments.

Comments

0

See more info http://dev.mysql.com/doc/refman/5.1/en/load-data.html

Load DATA INFILE 'file_name' into table table_name FIELDS TERMINATED BY '\t'

PHP code

$pdo = new PDO(connection settings);
$pdo->exec($mysql_query);

7 Comments

-1. MySQL can't magically determine how to delimit fields, etc. Besides, this is massive overkill for what OP is asking.
@AgentConundrum But in manual you can find TERMINATED BY 'string'. I have attached reference for more info.
@AgentConundrum before downvoting you should check answer.
LOAD DATA is a fine tool, but it's not the right one for this job. It requires a particular format that isn't present in the data here. For starters, fields aren't delimited by \t. Each field is on its own line, and there is more data on each line than needs to be stored. OP just needs to parse the file - trivially done in this case - and run a quick INSERT query. The only reason I haven't answered this myself is that it's extremely obviously homework, and standard practice for HW questions is to have OP show her work first, then guide her rather than just "sending teh codez".
@AgentConundrum I agree with you but I believe this is not good reason for downvoting because my solution is simple and maybe it's necessary to change format data if it's homework for getting the best result.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.