PHP/MySQL INSERT, tab delimited file

Posted on 2006-04-21
Last Modified: 2006-11-18
hello all,

I have the following code and it seems right to me, but it isnt actually performing the insert.  any ideas?


$server = mysql_connect('localhost', 'root') or die(mysql_error());
mysql_select_db ('paper') or die (mysql_error());

$readfile = file("customers.txt");

for ($k=0; $k<=count($readfile)-1; $k++) {
    $fields = split("\t",$readfile[$k]);
    //echo("$fields[0] and $fields[1] and $fields[3] and $fields[4] and $fields[5] and $fields[6] and $fields[7] and $fields[8] and $fields[9] and $fields[10] and $fields[11] and $fields[12] and $fields[13]<br>");
            $query = "INSERT INTO customers VALUES ('$fields[0]','$fields[1]','$fields[2]','$fields[3]','$fields[4]','$fields[5]','$fields[6]','$fields[7]','$fields[8]','$fields[9]','$fields[10]','$fields[11]','$fields[12]','$fields[13]')";
            //echo $query;


Question by:flow79
    LVL 49

    Expert Comment

    Try: mysql_query($query) or die(mysql_error());

    LVL 2

    Accepted Solution

    Give this a go, works fine here.

    Note: I've left my source as it was, so you'll need to change any passwords or usernames back to your own. You also might want to name your database columns a little better than in my example! :-)

    $server=@mysql_connect("localhost","root","mysql") or die(mysql_error());

    @mysql_select_db("test",$server) or die(mysql_error());


    for($k=0; $k<=count($readfile)-1; $k++)

          $query="insert into customers (id,field1,field2,field3,field4,field5,field6,field7,field8,field9,field10,field11,field12,field13,field14) VALUES (null,'$fields[0]','$fields[1]','$fields[2]','$fields[3]','$fields[4]','$fields[5]','$fields[6]','$fields[7]','$fields[8]','$fields[9]','$fields[10]','$fields[11]','$fields[12]','$fields[13]')";

          @mysql_query($query,$server) or die(mysql_error());

    @mysql_close() or die(mysql_error());
    LVL 2

    Expert Comment

    Oops, change:

    @mysql_close() or die(mysql_error());


    @mysql_close($server) or die(mysql_error());

    To ensure it closes the connection we created :-)

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Both Easy and Powerful How easy is PHP? (  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now