0

I am using the latest phpmyadmin. I have a database and within it is a whole bunch of tables. I want to load a semicolon delimited text file but it doesn't seem to work. Here is the sample of my text file where 55 is the field id. Field title is the country name and the last is the ordering of my countries.

55;Portugal;178
55;Puerto Rico;179
55;Qatar;180
55;Reunion;181
55;Romania;182
55;Russia;183

All i am trying to do is populate the country list so I dont have to enter it manually. Is there any other way of doing this? Please help!

Thank you so much

3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by rogenie
0

You might have to debug this a little but not much. This is how I would do it:

<?php
$data = "55;Portugal;178
55;Puerto Rico;179
55;Qatar;180
55;Reunion;181
55;Romania;182
55;Russia;183";

$queries = array();
foreach(explode("\n", $data) as $row)
{
    $line = "insert into tablename(fieldid, countryname, order) values(";
    foreach(explode(";", $row) as $col)
    {
        $line .= "'" . $col . "', ";
    }
    $queries[] = substr($line, 0, -2) . ");";
}

mysql_query("begin");
$commit = "commit";
$error = "";
$counter = 1;
foreach($queries as $query)
{
    if($commit == "commit") 
    {
        if(!mysql_query($query))
        {
            $error = "error in query " . $counter . ":" . mysql_error();
            $commit = "rollback";
        }
        $counter++;
    }
}
if($commit == "rollback")
{
    echo "transaction rolled back<br />" . $error;
}
mysql_query($commit);
?>

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.