hassank1 Posted October 31, 2008 Share Posted October 31, 2008 I've a text file that contains words as follows : word1 word2 word3 word4 word5 etc.. I want to create a table in mysql (id,word) ... and to place each word in a recrod.. ex : insert into table1 values('','word1'); insert into table1 values('','word2'); insert into table1 values('','word3'); is there a method or something to do that automatically ? because I won't do that manually for about 10,000 words :S !! thx Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/ Share on other sites More sharing options...
DarkWater Posted October 31, 2008 Share Posted October 31, 2008 <?php $filename = 'foo.txt'; foreach (file($filename) as $line) { $line = mysql_real_escape_string($line); $query = "INSERT INTO table1 (word) VALUES ('$line')"; //do query stuff } ?> That should be enough to get you started. Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679732 Share on other sites More sharing options...
Jeremysr Posted October 31, 2008 Share Posted October 31, 2008 This should do it: $lines = file('your_filename'); foreach ($lines as $line) { $line = chop($line); mysql_query("INSERT INTO table1 values('', '$line');"); } Edit: whoops, too late. It won't let me delete my post... Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679733 Share on other sites More sharing options...
hassank1 Posted October 31, 2008 Author Share Posted October 31, 2008 Ok thx guys I've about 10,000 words in my file.. so if I tried to insert all of them in a short time would that cause a problem or a may I get a warning from the hosting site ? Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679735 Share on other sites More sharing options...
DarkWater Posted October 31, 2008 Share Posted October 31, 2008 Well, I'm not your hosting company, so I don't know. I'd say that it should be fine, but you never know. You might as well try. Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679747 Share on other sites More sharing options...
.josh Posted November 1, 2008 Share Posted November 1, 2008 I did pretty much the exact same thing with about 27k lines (got an 'extended' English word list that I wanted to play around with), and it executed the script in like 2s. Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679763 Share on other sites More sharing options...
.josh Posted November 1, 2008 Share Posted November 1, 2008 oops. I just looked in my db and there's actually 227,000 rows. But anyways, it only took a few seconds to execute when I first inserted. And you only have 10,000. Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679766 Share on other sites More sharing options...
corbin Posted November 1, 2008 Share Posted November 1, 2008 If you're really worried about it, you could combine the queries to do like 100 inserts at a time. Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679780 Share on other sites More sharing options...
.josh Posted November 1, 2008 Share Posted November 1, 2008 You can't do more than 1 query at a time through php....or is there a way to insert multiple rows with 1 query? I'm no sql expert, just wondering. Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679796 Share on other sites More sharing options...
DarkWater Posted November 1, 2008 Share Posted November 1, 2008 INSERT INTO table (word) VALUES ('foo'), ('lol'), ('etc'); Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679802 Share on other sites More sharing options...
corbin Posted November 1, 2008 Share Posted November 1, 2008 Yeah, the example DarkWater provided was exactly what I meant. Oh, I think it's just MySQL by the way.... I don't think Oracle or MSSQL or any other dialect does it. Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679843 Share on other sites More sharing options...
DarkWater Posted November 1, 2008 Share Posted November 1, 2008 I'm 99% sure that it's NOT part of the SQL standard, so yeah. >_< Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679916 Share on other sites More sharing options...
PFMaBiSmAd Posted November 1, 2008 Share Posted November 1, 2008 There are also Prepared Statements and LOAD DATA LOCAL INFILE 'file_name' methods that would help when inserting large amounts of data. Quote Link to comment https://forums.phpfreaks.com/topic/130939-move-words-from-txt-file-to-mysql-table/#findComment-679963 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.