Jump to content

need help regarding bulk insert in php


m31hu1

Recommended Posts

Hi all, I'm new to this forum... and need help regarding a project that i am doing.
I'm trying to let sql store data from a text file, but the data won't display...
what am i missing here? Your help is greatly appreciated.

create table
<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}// Create database
if (mysql_query("CREATE DATABASE bulkdb",$con))
{
echo "Database created";
}
else
{
echo "Error creating database: " . mysql_error();
}

// Create table in bulkdb database
mysql_select_db("bulkdb", $con);
$sql = "CREATE TABLE Bulktable
(
fileID int NOT NULL AUTO_INCREMENT,
PRIMARY KEY(fileID),
FileName varchar(15),
Directory varchar(100)
)";


mysql_query($sql,$con);

?>

bulk insert
<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bulkdb", $con);
$bulkinsert = "BULK INSERT Bulktable
FROM 'test.txt'
WITH
(
FIELDTERMINATOR = ',',
ROWTERMINATOR = '\n'
)";

if ($bulkinsert){
echo "inserted";
}
else{
echo "not inserted";}

mysql_close($con);
?>


bulk select
<?php
$con = mysql_connect("localhost","root","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("bulkdb", $con);
$result = mysql_query("SELECT * FROM bulktable");

mysql_close($con);


echo "<table><tr><th colspan=\"3\">Results</th></tr><tr>";

while($row = mysql_fetch_array($result))
{
$img = $row['Directory'] . "" . $row['FileName'];
echo "<td><img src=\"$img\" height=\"60\" width=\"60\"></td>";

}

?>

test.txt
1.jpg,http://localhost/images/
2.jpg,http://localhost/images/
3.jpg,http://localhost/images/
4.jpg,http://localhost/images/
5.jpg,http://localhost/images/
6.jpg,http://localhost/images/
7.jpg,http://localhost/images/
8.jpg,http://localhost/images/

Regards,
Mei Hui
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.