473,385 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

import csv file into database

I have a code through which i want to import csv file into databse. But i am getting an error:
fopen() [function.fopen]: Filename cannot be empty and points on line 36(have marked in the code
Would appreciate if anyone could help me
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if ($_FILES["file"]["error"] > 0)
  3. {
  4.     echo "Error: " . $_FILES["file"]["error"] . "<br>";
  5. }
  6. else
  7. {
  8.     echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  9.     echo "Type: " . $_FILES["file"]["type"] . "<br>";
  10.     echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>";
  11.     //echo "Stored in: " . $_FILES["file"]["tmp_name"];
  12.     $a=$_FILES["file"]["tmp_name"];
  13.     //echo $a;
  14.  
  15.     $connect = mysql_connect('example.com','employee','172');
  16. if (!$connect) {
  17. die('Could not connect to MySQL: ' . mysql_error());
  18. }    
  19. //your database name
  20. $cid =mysql_select_db('employeemgt',$connect);
  21.  
  22. // path where your CSV file is located
  23. //define('CSV_PATH','C:/xampp/htdocs/');
  24. //<!-- C:\xampp\htdocs -->
  25. // Name of your CSV file
  26. $file = $a; 
  27.  
  28. if (($getfile = fopen($file, "r")) !== FALSE) { //line36
  29.          $data = fgetcsv($getfile, 1000, ",");
  30.    while (($data = fgetcsv($getfile, 1000, ",")) !== FALSE) {
  31.      //$num = count($data);
  32.        //echo $num;
  33.         //for ($c=0; $c < $num; $c++) {
  34.             $result = $data;
  35.             $str = implode(",", $result);
  36.             $slice = explode(",", $str);
  37.  
  38.             $col1 = $slice[0];
  39.             $col2 = $slice[1];
  40.             $col3 = $slice[2];
  41.             $col4 = $slice[3];
  42.             $col5 = $slice[4];
  43.             $col6 = $slice[5];
  44.             $col7 = $slice[6];
  45.             $col8 = $slice[7];
  46.             $col9 = $slice[8];
  47.             $col10 = $slice[9];
  48.             $col11 = $slice[10];
  49.             $col12 = $slice[11];
  50.             $col13 = $slice[12];
  51.             $col14 = $slice[13];
  52.             $col15 = $slice[14];
  53.             $col16 = $slice[15];
  54.             $col17 = $slice[16];
  55.             $col18 = $slice[17];
  56.             $col19 = $slice[18];
  57.             $col20= $slice[19];
  58.             $col21= $slice[20];
  59.             $col22 = $slice[21];
  60.             $col23 = $slice[22];
  61.             $col24 = $slice[23];
  62.             $col25 = $slice[24];
  63.             $col26 = $slice[25];
  64.             $col27 = $slice[26];
  65.             $col28 = $slice[27];
  66.             $col29 = $slice[28];
  67.             $col30 = $slice[29];
  68.             $col31 = $slice[30];
  69.             $col32 = $slice[31];
  70.  
  71.  
  72. $query = "INSERT INTO temp(id,firstname,lastname,displayname,nickname,email,email2,homephone,busnphone,fax,pager,mobphone,homestreet,homeaddress2,homecity,homestate,home,postalcode,homecountry,busadrs,busadrs2,buscity,busstate,buspostalcode,busicountry,jobtitle,deptt,orgnztn,notes,bday,webpage,webpage2,categories) VALUES('".$col1."','".$col2."','".$col3."','".$col4."','".$col5."','".$col6."','".$col7."','".$col8."','".$col9."','".$col10."','".$col11."','".$col12."','".$col13."','".$col4."','".$col15."','".$col16."','".$col17."','".$col18."','".$col9."','".$col20."','".$col21."','".$col22."','".$col23."','".$col24."','".$col25."','".$col26."','".$col27."','".$col28."','".$col29."','".$col30."','".$col31."','".$col32."')";
  73. $s=mysql_query($query, $connect );
  74. }
  75. }
  76.  
  77. echo "File data successfully imported to database!!";
  78. mysql_close($connect);
  79. }
  80. ?>
  81.  
Jun 26 '14 #1

✓ answered by Dormilich

hm, what do you think
Filename cannot be empty
means?

besides that, MySQL can import CSV directly.

1 1488
Dormilich
8,658 Expert Mod 8TB
hm, what do you think
Filename cannot be empty
means?

besides that, MySQL can import CSV directly.
Jun 27 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: matt | last post by:
I don't have a database available, so i'm writing an Image Gallery with a flat file database. I have an Add Page: Uploads Image file, and saves $filename and $caption to text file: captions.txt...
6
by: matt | last post by:
I am using a text file as a database, each field delimited by || I want to be able to print all lines to a page, then by selecting one (with, say, a radio button) it will load into the form at the...
4
by: Steve Holden | last post by:
I'm trying to load module code from a database, which stores for each module its full name, code, load date and a Boolean indicating whether it's a package or not. The following simple program:...
5
by: Steve Holden | last post by:
This is even stranger: it makes it if I import the module a second time: import dbimp as dbimp import sys if __name__ == "__main__": dbimp.install() #k = sys.modules.keys() #k.sort() #for...
2
by: aa | last post by:
How do I import Access database into MySQL?
2
by: Brian Huether | last post by:
I saved my website databgase to my home computer. I am setting up a local version of the site, and need to import the database. I have mysql and everything set up. But when I try to run the sql...
2
by: Eric Caron | last post by:
Hi I try to import file in a dataset. How I can do this ?
8
by: jtippen | last post by:
Can anyone point me in the right direction on how I can import a comma delimited webpage off a website into a table in Access 2003? If someone knows of an example of how to do this that would...
2
by: a.crowley | last post by:
I have a large set of Word documents that I wish to catalogue in an Access database. Each document has a set of file properties populated (title, subject, author, keywords, comments ect), so I'd...
11
by: kaisersose1995 | last post by:
Hi, I've got an import procedure working, using a standard import specification to import a .csv file into a temporary table. The problem i'm having is that i have 4 different sets of borrower...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.