husslela03 Posted December 3, 2009 Share Posted December 3, 2009 Hello, I would like to enter data from an array into a table in a mySQL database..how would I do that? Code that I have so far is below: <?php $con=mysql_connect("localhost","****","*****"); if(!con) { die('Could not connect: ' .mysql_error()); } if (mysql_query("CREATE DATABASE my_lingo",$con)) { echo "Database created"; } else { echo "Error creating database: " .mysql_error(); } //Create the table of words mysql_select_db("my_lingo", $con); $sql= "CREATE TABLE words ( wordID int NOT NULL AUTO_INCREMENT, PRIMARY KEY(wordID), word varchar(30) )"; $filename="words.txt"; $fp=fopen($filename, 'r'); $file_contents=fread($fp, filesize($filename)); fclose($fp); $lines=explode("\n", $filecontents); mysql_query($sql,$con); mysql_close($con); ?> Quote Link to comment https://forums.phpfreaks.com/topic/183888-inserting-data-in-an-array-into-a-mysql-table-using-php/ Share on other sites More sharing options...
Psycho Posted December 3, 2009 Share Posted December 3, 2009 Why explode $file_contents into an array? Just store it as-is in the the database and then explode when you use it later if needed. Otherwise, you can use serialize() and unserialize() to contert/unconvert an array. Quote Link to comment https://forums.phpfreaks.com/topic/183888-inserting-data-in-an-array-into-a-mysql-table-using-php/#findComment-970746 Share on other sites More sharing options...
husslela03 Posted December 3, 2009 Author Share Posted December 3, 2009 well, I have a text file of words that are in a list format, and i need to initialize the table from a script. I created the table for the words, but I just need to insert the words in the table using the INSERT INTO but I want to take what's in the text file as entries in the table. Quote Link to comment https://forums.phpfreaks.com/topic/183888-inserting-data-in-an-array-into-a-mysql-table-using-php/#findComment-970758 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.