Solved

PHP: Csv to Mysql

Posted on 2013-01-19
7
390 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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:maccaj51
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
@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
Comment Utility
All sorted Ray... Many thanks
0
 
LVL 108

Expert Comment

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

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

743 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

15 Experts available now in Live!

Get 1:1 Help Now