Jump to content

Inserting data in an array into a mySQL table using php


husslela03

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.