Solved

PHP: Csv to Mysql

Posted on 2013-01-19
7
392 Views
Last Modified: 2013-01-22
Hi Experts,

I have the following code which takes a csv file and displays it as a table.

What I would like to do it is take all rows after row #1 and insert it into a mysql database.

Could someone please help me with this

Many Thanks

ini_set("auto_detect_line_endings", true);
$csv = test.csv;

$fpo = fopen($csv, 'r');
if (!$fpo) die("CANNOT OPEN $csv");
$top = fgetcsv($fpo);

// READ THE REST OF THE ROWS
$arr = array();
while (!feof($fpo))
{
    $arr[] = fgetcsv($fpo);
}

// CREATE AN HTML DOCUMENT
$htm = '<h2>' . $csv . '</h2>' . PHP_EOL;

// CREATE A TABLE
$htm .= '<table width="100%">' . PHP_EOL;

// THE TOP ROW IN BOLD
$htm .= '<tr>' . PHP_EOL;
foreach ($top as $dat)
{
    $htm .= '<td>' . "$dat" . '</td>' . PHP_EOL;
}
$htm .= '</tr>' . PHP_EOL;

// THE OTHER ROWS
foreach ($arr as $row)
{
	
if (empty($row))
{}
else
{	
    $htm .= '<tr>' . PHP_EOL;
	foreach ($row as $dat)
	{
	    $htm .= '<td>' . $dat . '</td>' . PHP_EOL;
	}
    $htm .= '</tr>' . PHP_EOL;
}
}

// CLOSE THE HTML DOCUMENT
$htm .= '</table>';

echo $htm;
	
	
	

Open in new window

0
Comment
Question by:maccaj51
  • 3
  • 3
7 Comments
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38797279
This is how I have done it.  Take very careful note of lines 38-43.  The assumption is that the CSV file has the DB table's column names in the first row of the CSV file.  If that is not true for you, you may want to remove that part of the script and make other arrangements to get the column names.

<?php // RAY_csv_to_db.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;  //READABILITY FOR var_dump()


// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";

// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB CONNECTION: ";
    echo "<br/> $errmsg <br/>";
}

// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
    $errmsg = mysql_errno() . ' ' . mysql_error();
    echo "<br/>NO DB SELECTION: ";
    echo "<br/> $errmsg <br/>";
    die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES


// TEST DATA
$csv = "PATH/TO/YOUR/file.csv";
$fpo = fopen($csv, 'r');
if (!$fpo ) die("CRAP.  CANNOT OPEN $csv");

// GET THE FIELD NAMES FROM THE TOP OF THE CSV FILE
$top = fgetcsv($fpo);
$cnt = count($top);

// SET UP KEY NAMES FOR USE IN OUR QUERY
$query_cols = implode(',', $top);

// SET A ROW COUNTER
$counter = 0;

// KEEP TRACK OF ROWS THAT HAVE THE WRONG NUMBER OF FIELDS
$errors = array();

// LOOP THROUGH THE CSV RECORDS PERFORMING CERTAIN TESTS
while (!feof($fpo))
{
    $counter++;

    // GET A RECORD
    $csvdata = fgetcsv($fpo);

    // SKIP OVER EMPTY ROWS
    if (empty($csvdata)) continue;

    // CHECK THE NUMBER OF FIELDS
    if ($cnt != count($csvdata))
    {
        $errors[] = $counter;
        continue;
    }

    // MAYBE ASSIGN KEYS TO THE ROW OF FIELDS - ACTIVATE THIS TO SEE THE ASSOCIATIVE ARRAY
    //  $csvdata = array_combine($top, $csvdata);
    //  var_dump($csvdata);

    // ESCAPE THE INFORMATION FOR USE IN THE QUERY
    foreach ($csvdata as $ptr => $value)
    {
        $csvdata[$ptr] = mysql_real_escape_string($value);
    }

    // SET UP VALUE FIELDS
    $query_data = "'" . implode("', '", $csvdata) . "'";

    // SET UP A QUERY
    $sql = "REPLACE INTO myTable ( $query_cols ) VALUES ( $query_data )";

    // RUN THE QUERY HERE....
    var_dump($sql);
}


// SHOW THE NUMBER OF ROWS PROCESSED
echo "<br/>RECORDS PROCESSED $counter " . PHP_EOL;

// SHOW THE NUMBERS OF THE ROWS WITH THE WRONG NUMBER OF FIELDS
if (count($errors))
{
    echo "<br/>ROWS WITH THE WRONG NUMBER OF FIELDS: " . PHP_EOL;
    var_dump($errors);
}

Open in new window

0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 38800311
Try mysql "load data" it might help you

just an eg

mysql> LOAD DATA LOCAL INFILE ‘/var/www/site/site_users.csv’
INTO TABLE site_users FIELDS
TERMINATED BY ‘;’
ENCLOSED BY ‘”‘
LINES TERMINATED BY ‘\n’
IGNORE 1 LINES;
0
 

Author Comment

by:maccaj51
ID: 38800590
Hi Ray,

Thanks so much for that script...

It seems to be doing everything it should... including echoing the correct import.

However it doesnt insert into the database...
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 

Author Comment

by:maccaj51
ID: 38800603
Attached is a csv files with the headings for the fields...

I have added this code to the top of the php script:
ini_set("auto_detect_line_endings", true);

And it outputs the records followed by correct number of "RECORDS PROCESSED"

But doesnt insert them into the table.

Am I doing something wrong?

Many Thanks
Workbook2.csv
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38800833
@maccaj51: The code I furnished is a teaching example.  I do not have your data base and tables set, so there is no way for me to run the query.  So the answer is , "No, you're not doing anything wrong."  You just have to go into the script, find the code on line 85 and 86 and add your own call to your query handler at that point.
0
 

Author Comment

by:maccaj51
ID: 38804549
All sorted Ray... Many thanks
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 38805032
Great!  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

813 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

12 Experts available now in Live!

Get 1:1 Help Now