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

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.