You are here: Populating a mysql db from txt file with php « All PHP « IT news, forums, messages
Populating a mysql db from txt file with php

Posted by Sonoman on 09/28/63 11:45

Hi all,
I have written the code below to take a text file and build a series of
queries to populate my database with data in the text file. I am having a
problem with getting the data into my db. Here is a small sample of the text
file followed by my PHP code and the error last. The offending line (Line
48) is marked so you dont have to count. I got the text file from a MS
Access DB, may be there is a way to use this instead of the exported txt
file? Thanks for the help.

1?-1?11?Sector 11--Agriculture, Forestry, Fishing and Hunting?
2?1?111?Crop Production?
3?2?1111?Oilseed and Grain Farming?
4?3?11111?Soybean Farming?
6?3?11112?Oilseed (except Soybean) Farming?
7?3?11113?Dry Pea and Bean Farming?
8?3?11114?Wheat Farming?
10?3?11115?Corn Farming?
11?3?11116?Rice Farming?
13?3?11119?Other Grain Farming?
14?13?111191?Oilseed and Grain Combination Farming?
15?13?111199?All Other Grain Farming?
16?2?1112?Vegetable and Melon Farming?
18?2?1113?Fruit and Tree Nut Farming?
19?18?11131?Orange Groves?
21?18?11132?Citrus (except Orange) Groves?
22?18?11133?Noncitrus Fruit and Tree Nut Farming?
23?22?111331?Apple Orchards?
24?22?111332?Grape Vineyards?
25?22?111333?Strawberry Farming?

<?php
$hostname="myserver";
$username="myusername";
$password="mypassword";
$dbname="mydbname";
$usertable="mytablename";

$filename = "Industries.txt";
$fd = fopen ($filename, "r");
$contents = fread ($fd,filesize ($filename));

fclose ($fd);
$delimiter = "?";
$splitcontents = explode($delimiter, $contents);
$counter = 0;

foreach ( $splitcontents as $mydata )
{
if ($counter == 0)
{
$populate = "INSERT INTO industries(id,parentID,code,title,description)
VALUES(";
}
$counter = $counter+1;

if ($counter == 4)
{
$populate = $populate."'";
}

$populate = $populate.$mydata;

if ($counter == 4)
{
$populate = $populate."'";
}

$populate = $populate.",";

if ($counter == 4)
{
$counter = 0;
$populate = $populate."'";
$populate = $populate.$mydata;
$populate = $populate."'";
$populate = $populate.")";
}

$result = mysql_query($populate) or die('Could not insert data:
'.mysql_error()); <-- Line 48
echo $result."result is: <br>";
}

?>


Warning: mysql_query(): Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2) in /home/content/f/c/a/fcarpio/html/populate.php on
line 48

Warning: mysql_query(): A link to the server could not be established in
/home/content/f/c/a/fcarpio/html/populate.php on line 48
Could not insert data: Can't connect to local MySQL server through socket
'/tmp/mysql.sock' (2)

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация