I have this csv export code.
the export I working well but my only problem is that the result is showing only in one column
the problem happens only when I try to convert the file to UTF-8,
is there another way around this?
$link = mysqli_connect(MYSQL_HOST, MYSQL_USER, MYSQL_PWD, MYSQL_DB);
$link->set_charset("utf8");
$sql = "SELECT * FROM weekevents";
$query = mysqli_query($link,$sql);
$col = "col1,col2,col3,col4 \n";//Column headers
foreach ($query as $record){
$cam = explode(';', $record['cam']);
$col .= $record['weekday'].','.$record['week']
;
}
$filename = date("m-Y");
$csv_handler = fopen ('C:/xampp/htdocs/rge/main-rtl/uploads/שיבוץ חודש -'.$filename.'.csv','w');
fwrite ($csv_handler, $col);
fclose ($csv_handler);
echo 'Data saved to csvfile.csv';
Related
I've had no issue with the first line of a CSV being read upon upload and then inserted into the database, but the multi line task is having issues. I can now get it to print an array dump on the web page that shows all lines of the CSV but it's still only inserting the first line into my database. I've worked my code around and came up with this:
if(isset($_POST['submit']))
{
ini_set('auto_detect_line_endings', true);
$file = $_FILES["file"]["tmp_name"];
$handle = fopen($file, "r");
while(!feof($handle)){
$filesop = print_r(fgetcsv($handle, 0, ","));
}
$coldata = array();
I have this code for calling the queries (there are other queries after this, but this is the main one that puts into that staging table):
$tablenames = array("staging"/*,"clients","meters","tests","costs","workorders"*/);
for($tableno = 0;$tableno < sizeof($tablenames);$tableno++){
$q = "";
$q2 = "";
$q3 = "";
$q4 = "";
$q5 = "";
$q6 = "";
$col_list = '`'.str_replace(',','`,`',$table_cols[$tableno]).'`';
$q .= "INSERT INTO ".$tablenames[$tableno]." (".$col_list.") VALUES (";
$last_id = mysqli_insert_id($connect);
$cols = explode(",",$table_cols[$tableno]);
$data = array();
foreach($cols as $key => $fldname) {
$data[] = "'".$coldata[$fldname]."'";
}
/*INSERT INTO STAGING TABLE - INITAL CSV UPLOAD*/
$q .= implode(",",$data).");";
Am I doing something wrong in the queries that's causing it to not insert all lines of the CSV into the db?
there is time stamp in my actual csv file but while loading it into mysql every thing is being loaded except the time stamp which remained as null. i am using mysql in phpadmin(i.e xampp),
code used is:
<?php
// specify connection info
$connect = mysql_connect('localhost','root','');
if (!$connect)
{
die('Could not <span id="IL_AD1" class="IL_AD">
connect to</span> MySQL: ' . mysql_error());
}
$cid =mysql_select_db('test',$connect); //specify db name
define('CSV_PATH','C:\Users\vijay\Desktop');
$csv_file = CSV_PATH . "\probe.csv"; // Name of your CSV file
$csvfile = fopen($csv_file,"r");
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile))
{
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv["app_name"] = $csv_array[0];
$insert_csv["user_id"] = $csv_array[1];
$insert_csv["timestamp"] = $csv_array[2];
$insert_csv["event"] = $csv_array[3];
$insert_csv["feature"] = $csv_array[4];
$insert_csv["action_name"] = $csv_array[5];
$insert_csv["qoe_rate"] = $csv_array[6];
$insert_csv["qoe_rate_description"] = $csv_array[7];
$query = "insert into csvdata2 (app_name,user_id,timestamp,event,feature,action_name,qoe_rate,qoe_rate_description) VALUES('$insert_csv[app_name]','$insert_csv[user_id]',$insert_csv[timestamp],'$insert_csv[event]','$insert_csv[feature]','$insert_csv[action_name]',$insert_csv[qoe_rate],'$insert_csv[qoe_rate_description]')";
$n=mysql_query($query);
echo $n;
$i++;
}
fclose($csvfile);
echo "file data successfully imported to database!!";
mysql_close($connect);
?>
How to include the timestamp code so that i would get the exact timestamp data loaded into table without disturbing the other entries...thanks in advance
You are missing quotes
Here
$insert_csv[timestamp]
$insert_csv[qoe_rate]
Try now
$query = "insert into csvdata2 (app_name,user_id,timestamp,event,feature,action_name,qoe_rate,qoe_rate_description) VALUES('$insert_csv[app_name]','$insert_csv[user_id]','$insert_csv[timestamp]','$insert_csv[event]','$insert_csv[feature]','$insert_csv[action_name]','$insert_csv[qoe_rate]','$insert_csv[qoe_rate_description]')";
and follow what #Plenka mentioned in comment
I have some code here that exports to a csv file. I connected to the database and saw that there was well over 30 entries or so. But I'm only getting one in the csv file. The top part gets the headings for the file and the bottom the values, the values array is only returning one array and putting it into the file. I'm not sure what's causing this issue. Anny suggestions would be greatly appreciated.
<?php
$FileName = "mro_export_".date("Y-m-d_H-i",time()).".csv";
$file = fopen($FileName,"w");
$sql = mysql_query("SELECT * FROM `$table` LIMIT 11");
$row = mysql_fetch_assoc($sql);
// Save headings alon
$HeadingsArray=array();
foreach($row as $name => $value){
$HeadingsArray[]=$name;
}
fputcsv($file,$HeadingsArray);
$ValuesArray=array();
foreach($row as $name => $value){
$ValuesArray[]=$value;
}
fputcsv($file,$ValuesArray);
fclose($file);
header("Location: $FileName");
?>
You need to call mysql_fetch_assoc in a loop to get all the rows.
<?php
$FileName = "mro_export_".date("Y-m-d_H-i",time()).".csv";
$file = fopen($FileName,"w");
$sql = mysql_query("SELECT * FROM `$table` LIMIT 11") or die(mysql_error());
$first_line = true;
while ($row = mysql_fetch_assoc($sql)) {
if ($first_line) {
// Save headings alon
fputcsv($file, array_keys($row));
$first_line = false;
}
fputcsv($file, array_values($row));
}
fclose($file);
header("Location: $FileName");
?>
I am using the following to upload CSV to a table in MYSQL database.
Question: How do I bypass the 1st field? - as in ignore the header - which is currently being saved in my table?
Code:
<?php
/********************************/
/* Code at http://legend.ws/blog/tips-tricks/csv-php-mysql-import/
/* Edit the entries below to reflect the appropriate values
/********************************/
$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername ="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";
/********************************/
/* Would you like to add an ampty field at the beginning of these records?
/* This is useful if you have a table with the first field being an auto_increment integer
/* and the csv file does not have such as empty field before the records.
/* Set 1 for yes and 0 for no. ATTENTION: don't set to 1 if you are not sure.
/* This can dump data in the wrong fields if this extra field does not exist in the table
/********************************/
$addauto = 0;
/********************************/
/* Would you like to save the mysql queries in a file? If yes set $save to 1.
/* Permission on the file should be set to 777. Either upload a sample file through ftp and
/* change the permissions, or execute at the prompt: touch output.sql && chmod 777 output.sql
/********************************/
$save = 1;
$outputfile = "output.sql";
/********************************/
if(!file_exists($csvfile)) {
echo "File not found. Make sure you specified the correct path.\n";
exit;
}
$file = fopen($csvfile,"r");
if(!$file) {
echo "Error opening data file.\n";
exit;
}
$size = filesize($csvfile);
if(!$size) {
echo "File is empty.\n";
exit;
}
$csvcontent = fread($file,$size);
fclose($file);
$con = #mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error());
#mysql_select_db($databasename) or die(mysql_error());
$lines = 0;
$queries = "";
$linearray = array();
foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t");
$line = str_replace("\r","",$line);
/************************************
This line escapes the special character. remove it if entries are already escaped in the csv file
************************************/
$line = str_replace("'","\'",$line);
/*************************************/
$linearray = explode($fieldseparator,$line);
$linemysql = implode("','",$linearray);
if($addauto)
$query = "insert into $databasetable values('','$linemysql');";
else
$query = "insert into $databasetable values('$linemysql');";
$queries .= $query . "\n";
#mysql_query($query);
}
#mysql_close($con);
if($save) {
if(!is_writable($outputfile)) {
echo "File is not writable, check permissions.\n";
}
else {
$file2 = fopen($outputfile,"w");
if(!$file2) {
echo "Error writing to the output file.\n";
}
else {
fwrite($file2,$queries);
fclose($file2);
}
}
}
echo "Found a total of $lines records in this csv file.\n";
?>
I has been did the same purpose at last week. Please refer this code. It may be help you.
if (isset($_POST['submit'])) {
//Path of Uploading file
$target = "upload_files/results/";
$filename = $_FILES['upload_result']['name'];
if(file_exists($target.$filename)) {
unlink($target.$filename);
}
move_uploaded_file($_FILES['upload_result']['tmp_name'], $target.$filename);
//Store the Detail into the Master table...[class_master]
$new_master = mysql_query("INSERT INTO result_master(classname,sectionname,examname,filename) Values('$class','$section','$examname','$filename')");
$filename1 = explode(".",$filename);
$file = preg_replace("/[^a-zA-Z0-9]+/", "", $filename1[0]);
// get structure from csv and insert db
ini_set('auto_detect_line_endings',TRUE);
$handle = fopen($target.$filename,'r');
// first row, structure
if ( ($data = fgetcsv($handle) ) === FALSE ) {
echo "Cannot read from csv $file";die();
}
$fields = array();
$field_count = 0;
for($i=0;$i<count($data); $i++) {
$f = trim($data[$i]);
if ($f) {
// normalize the field name, strip to 20 chars if too long
$f = substr(preg_replace ('/[^0-9a-z]/', '_', $f), 0, 20);
$field_count++;
$fields[] = $f.' VARCHAR(50)';
}
}
$drop = mysql_query("Drop table IF EXISTS $file;");
$sql = "CREATE TABLE $file (" . implode(', ', $fields) . ')';
echo $sql . "<br /><br />";
$db = mysql_query($sql);
while ( ($data = fgetcsv($handle) ) !== FALSE ) {
$fields = array();
for($i=0;$i<$field_count; $i++) {
$fields[] = '\''.addslashes($data[$i]).'\'';
}
$sql = "Insert into $file values(" . implode(', ', $fields) . ')';
echo $sql;
$db = mysql_query($sql);
}
fclose($handle);
ini_set('auto_detect_line_endings',FALSE);
}
You either assume that there is ALWAYS a header, or that any header will be commented by some character, I will show you the first. You can just wrap the insertion lines in a conditional block to check for this.
if( $lines != 0 ) // or $linemysql[0][0] == '#' (assuming # is a "comment")
{
if($addauto)
$query = "insert into $databasetable values('','$linemysql');";
else
$query = "insert into $databasetable values('$linemysql');";
}
That being said, PLEASE! Do not ever use the code you posted in any internet facing application, you are putting user-provided data directly into the database, so it would be trivial to make a csv file containing SQL injection attack and change your mysql password, steal your data, kill your cat or delete everything. You should also check the number of fields and such, what happens if the csv contains lines without the correct number of fields ?
Read up on http://en.wikipedia.org/wiki/SQL_injection
and also http://php.net/PDO
Use this code in removing the header in the csv file and upload the data in the database:
$header= 1;
if (($handle = fopen("data.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($header== 1)
{
$header++;
continue;
}
//do your code here saving into the database
}
fclose($handle);
}
I have tried the following code but getting some errors. Here I can read the input file but I am getting the following error:Deprecated: Function split() is deprecated in C:\wamp\www\aaj2\index.php on line 63.
O/P: Found a total of 5124 records in this csv file.
<?php
$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";
$addauto = 0;
$save = 1;
$outputfile = "output.sql";
if(!file_exists($csvfile)) { echo "File not found. Make sure you specified the correct path.\n"; exit; }
$file = fopen($csvfile,"r");
if(!$file) { echo "Error opening data file.\n"; exit; }
$size = filesize($csvfile);
if(!$size) { echo "File is empty.\n"; exit; }
$csvcontent = fread($file,$size);
fclose($file);
$con = #mysql_connect($databasehost,$databaseusername,$databasepassword) or die(mysql_error()); #mysql_select_db($databasename) or die(mysql_error());
$lines = 0; $queries = ""; $linearray = array();
foreach(split($lineseparator,$csvcontent) as $line) {
$lines++;
$line = trim($line," \t"); $line = str_replace("\r","",$line); /************************************ This line escapes the special character. remove it if entries are already escaped in the csv file ************************************/ $line = str_replace("'","\'",$line); /*************************************/ $linearray = explode($fieldseparator,$line); $linemysql = implode("','",$linearray); if($addauto) $query = "insert into $databasetable values('','$linemysql');"; else $query = "insert into $databasetable values('$linemysql');"; $queries .= $query . "\n";
#mysql_query($query); }
#mysql_close($con);
if($save) { if(!is_writable($outputfile)) { echo "File is not writable, check permissions.\n"; } else { $file2 = fopen($outputfile,"w");
if(!$file2) { echo "Error writing to the output file.\n"; } else { fwrite($file2,$queries); fclose($file2); } } }
echo "Found a total of $lines records in this csv file.\n";
?>
EDIT : Error : File is not writable, check permissions. Found a total of 5124 records in this csv file.
Several tips:
Don't use the deprecated ext/mysql, when you can use ext/mysqli or PDO.
Don't read the entire csv file into a PHP variable. What happens when the file is 500MB?
Don't write custom PHP code to parse csv data, when you can use the builtin function fgetcsv().
Don't create a new SQL statement for every row in the data, when you can use prepared statements.
Don't interpolate data from an external file into SQL statements. This risks SQL injection vulnerabilities, just like when you interpolate untrusted user input.
Don't parse and insert csv data row by row, when you can use MySQL's LOAD DATA INFILE command. It's 20x faster than inserting row by row.
Here's a simpler solution:
<?php
$databasehost = "localhost";
$databasename = "test";
$databasetable = "sample";
$databaseusername="test";
$databasepassword = "";
$fieldseparator = ",";
$lineseparator = "\n";
$csvfile = "filename.csv";
if(!file_exists($csvfile)) {
die("File not found. Make sure you specified the correct path.");
}
try {
$pdo = new PDO("mysql:host=$databasehost;dbname=$databasename",
$databaseusername, $databasepassword,
array(
PDO::MYSQL_ATTR_LOCAL_INFILE => true,
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
)
);
} catch (PDOException $e) {
die("database connection failed: ".$e->getMessage());
}
$affectedRows = $pdo->exec("
LOAD DATA LOCAL INFILE ".$pdo->quote($csvfile)." INTO TABLE `$databasetable`
FIELDS TERMINATED BY ".$pdo->quote($fieldseparator)."
LINES TERMINATED BY ".$pdo->quote($lineseparator));
echo "Loaded a total of $affectedRows records from this csv file.\n";
?>
I tested this with PHP 5.3.26 on a Mac, connecting to MySQL 5.6.14 on Linux.
Try this:
<?php
// specify connection info
$connect = mysql_connect('localhost','root','12345');
if (!$connect)
{
die('Could not <span id="IL_AD1" class="IL_AD">
connect to</span> MySQL: ' . mysql_error());
}
$cid =mysql_select_db('test',$connect); //specify db name
define('CSV_PATH','C:/wamp/www/csvfile/'); // specify CSV file path
$csv_file = CSV_PATH . "infotuts.csv"; // Name of your CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
while (!feof($csvfile))
{
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
$insert_csv['ID'] = $csv_array[0];
$insert_csv['name'] = $csv_array[1];
$insert_csv['email'] = $csv_array[2];
$query = "INSERT INTO csvdata(ID,name,email)
VALUES('','".$insert_csv['name']."','".$insert_csv['email']."')";
$n=mysql_query($query, $connect );
$i++;
}
fclose($csvfile);
echo "File data successfully imported to database!!";
mysql_close($connect); // closing connection
?>
I think you need to convert CSV file in collection first. After that you can add loop and call insert queries to insert data.
Here you will get code to convert CSV file into collection array.
best-way-to-upload-and-read-csv-file-in-php
**Data Import And Export**
**CSV To Mysql AND Mysql To CSV Using Mysqli**
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form method="post">
<button type="submit" name="btn_export">Data Export</button>
<button type="submit" name="btn_import">Data Import</button>
</form>
<?php
$host = "localhost";
$uname = "root";
$pass = "";
$database = "demo"; //Change Your Database Name
$conn = new mysqli($host, $uname, $pass, $database)or die("No Connection");
echo mysql_error();
//MYSQL MYADDMINPHP TO CSV
if (isset($_REQUEST['btn_export']))
{
$data_op = "";
$sql = $conn->query("select * from users"); //Change Your Table Name
while ($row1 = $sql->fetch_field())
{
$data_op .= '"' . $row1->name . '",';
}
$data_op .="\n";
while ($row = $sql->fetch_assoc())
{
foreach ($row as $key => $value)
{
$data_op .='"' . $value . '",';
}
$data_op .="\n";
}
$filename = "Database.csv"; //Change File type CSV/TXT etc
header('Content-type: application/csv'); //Change File type CSV/TXT etc
header('Content-Disposition: attachment; filename=' . $filename);
echo $data_op;
exit;
}
//CSV To MYSQL MYADDMINPHP
if (isset($_REQUEST['btn_import']))
{
$filename = 'Database.csv';
$fp = fopen($filename, "r");
while (($row = fgetcsv($fp, "40", ",")) != FALSE)
{
$sql = "INSERT INTO users (name,pass,city,id) VALUES('" . implode("','", $row) . "')";
if (!$conn->query($sql))
{
echo '<br>Data No Insert<br>';
}
}
fclose($fp);
}
?>
</body>
</html>