Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP: Csv to Mysql

Posted on 2013-01-19
7
Medium Priority
?
401 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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
Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

 

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 111

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 111

Expert Comment

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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, we’ll look at how to deploy ProxySQL.
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

704 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