Insert CSV file to database in PHP

Description

The following code shows how to insert CSV file to database.

Example


<?php/*from ww  w .  j  a v  a2  s.  c o  m*/
    // Connect to the MySQL server and select the corporate database
    $mysqli = new mysqli("localhost","root","","test");

    // Open and parse the sales.csv file
    $fh = fopen("data.csv", "r");

    while ($line = fgetcsv($fh, 1000, ","))
    {
        $id = $line[0];
        $client_id = $line[1];
        $order_time = $line[2];
        $sub_total = $line[3];
        $shipping_cost = $line[4];
        $total_cost = $line[5];

        // Insert the data into the sales table
        $query = "INSERT INTO sales SET id='$id',
            client_id='$client_id', order_time='$order_time',
            sub_total='$sub_total', shipping_cost='$shipping_cost',
            total_cost='$total_cost'";

        $result = $mysqli->query($query);
    }

    fclose($fh);
    $mysqli->close();
?>




















Home »
  PHP Tutorial »
    MySQL »




MySQLi
MySQLi Object Oriented