Jump to content

Recommended Posts

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);

?>

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.