Solved

PHP: Csv to Mysql

Posted on 2013-01-19
7
400 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 110

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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

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 110

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 110

Expert Comment

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

Featured Post

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

632 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