Jump to content

Archived

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

m31hu1

need help regarding bulk insert in php

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

Share this post


Link to post
Share on other sites

×

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.