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
Related
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';
got stuck in a piece of code and needed some insights. I have several csv files in a folder and need their data inserted in mysql.
i tried this post but its not working for multiple files.
Below is my code:
<?php
//database connection details
$connect = mysql_connect('localhost','test','pass');
if (!$connect) {
die('Could not connect to MySQL: ' . mysql_error());
}
//your database name
$cid =mysql_select_db('db',$connect);
// path where your CSV file is located
define('CSV_PATH','/root/path/uploads/');
// Name of your CSV file
$filename= "1";
do {
echo $filename;
$filename++;
$csv_file = CSV_PATH . $filename.".csv";
if (($handle = fopen($csv_file, "r")) !== FALSE) {
fgetcsv($handle);
while (($data = fgetcsv($handle, 10000, ",")) !== FALSE) {
$num = count($data);
for ($c=0; $c < $num; $c++) {
$col[$c] = $data[$c];
}
$col1 = $col[0];
$col2 = $col[1];
$col3 = $col[2];
$col4 = $col[3];
$col5 = $col[4];
$col6 = $col[5];
$col7 = $col[6];
$col8 = $col[7];
$col9 = $col[8];
$col10 = $col[9];
$col11 = $col[10];
// SQL Query to insert data into DataBase
$query = "INSERT INTO csvtbl(sl_no,id,poke_no,star,p_name,cp,iv,primary1,secondary,candy,level) VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."','".$col8."','".$col9."','".$col10."','".$col11."')";
$s = mysql_query($query, $connect );
}
fclose($handle);
}
echo "Account added successfully imported to database!!<br>";
//1 column entry query for another table
$query1 = "INSERT INTO account(id) VALUES('".$col1."')";
$a = mysql_query($query1, $connect );
echo "New Account added successfully imported to database!!";
mysql_close($connect);
} while ($filename <= 1000);
?>
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>
Okay so I have a php script to insert data into a mysql table it looks like this:
<?php
$connect = mysql_connect('localhost','my_username','my_pass');
if (!$connect) {die('Could not connect to MySQL: ' . mysql_error());}
$cid =mysql_select_db('geolocation',$connect);
// supply your database name
define('CSV_PATH','/var/www/geolocation/');
// path where your CSV file is located
$csv_file = CSV_PATH . "new_Location.csv"; // Name of your CSV file
$csvfile = fopen($csv_file, 'r');
$theData = fgets($csvfile);
$i = 0;
$k = 0;
while (!feof($csvfile)) {
$csv_data[] = fgets($csvfile, 1024);
$csv_array = explode(",", $csv_data[$i]);
$insert_csv = array();
if(isset($csv_array[0]) && isset($csv_array[1]) && isset($csv_array[2]) && isset($csv_array[3]) && isset($csv_array[4]) && isset($csv_array[5]) && isset($csv_array[6]) && isset($csv_array[7]) && isset($csv_array[8])){
$insert_csv['ID'] = $csv_array[0];
$insert_csv['country'] = $csv_array[1];
$insert_csv['region'] = $csv_array[2];
$insert_csv['city'] = $csv_array[3];
$insert_csv['postalCode'] = $csv_array[4];
$insert_csv['latitude'] = $csv_array[5];
$insert_csv['longitude'] = $csv_array[6];
$insert_csv['other'] = $csv_array[7];
$insert_csv['another'] = $csv_array[8];
$query = "INSERT INTO City (locId, country, region, city, postalCode, latitude, longitude) VALUES('".$insert_csv['ID']."','".$insert_csv['country']."','".$insert_csv['region']."','". $insert_csv['city']."','".$insert_csv['postalCode']."','".$insert_csv['latitude'].",,'".$in sert_csv['longitude']."'')";
$n=mysql_query($query, $connect );
$i++;
}
if($k==1000){
echo $i . " \n";
$k = 0;
}
$k++;
}
fclose($csvfile);
echo "File data successfully imported to database!!";
mysql_close($connect);
?>
now when I run the script everything seems to work properly and I get echo $i every once in a while and eventually the script ends with no errors or anything however when I look into my mysql (through phpmyadmin) I do not see any of the rows added to the table... I have also used another modified version of this script in the past and it worked perfectly (other than slow), However I'm still pretty green when it comes to mysql and can't seem to figure out what's going on... here is my table format
locId --> int(11)
country --> varchar(2)
region --> varchar(2)
city --> varchar(50)
postalCode --> varchar(8)
latitude --> varchar(10)
longitude --> varchar(10)
and also a sample from the csv file:
46,CK,NA,NA,NA,-21.2333,-159.7667,NA,NA
any help with why this isn't workign or even how I can go about debugging it would be very much appreciated!
also note (I do not wish to have the last to value in the csv in my table) so those are ommited from the insert statement on purpose
i do something like this:
$file = fopen("$link_file","r")or die("file dont exist");
while (!feof($file )){
$campo = fgetcsv($file,4096,",");
$loadsql = "INSERT INTO temporal_table(id,state,etc) VALUES ('$campo[0]','$campo[1]','etc');";
mysql_query($loadsql) or die(mysql_error());
}
fclose($file );
i dont see you using fgetcsv function.
and, did you try to echo the data in the while?
use something like this:
echo $i." line query ".$query." <br>";
EDIT
try this:
$csv_file = CSV_PATH . "new_Location.csv";
$file = fopen("$csv_file","r")or die("file error");
while (!feof($file)){
$insert_csv = fgetcsv($file,4096,",");
$query = "INSERT INTO City (
locId,
country,
region,
city,
postalCode,
latitude,
longitude //removed all the simple quotes..
) VALUES (
'$insert_csv[0]',
'$insert_csv[1]',
'$insert_csv[2]',
'$insert_csv[3]',
'$insert_csv[4]',
'$insert_csv[5]',
'$insert_csv[6]'
)";
mysql_query($query) or die(mysql_error());
}
fclose($csvfile);