Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP: Csv to Mysql

Posted on 2013-01-19
7
Medium Priority
?
403 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

927 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