Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Import CSV file into MySQL database using PHP

Posted on 2011-03-16
Medium Priority
Last Modified: 2013-12-13
Dear reader,

I'm trying to import 2 CSV files into temptables in my MySQL database.
In my local development environment it works fine, but when I upload the file to the server I get an error saying the query cannot be executed. It concerns the query below.

$import = "INSERT INTO temp_grau1 (CODI,CONCEPTA,TAMANY,PVP,PREU_ESPECIAL,ANYADA,STOCK) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]')";

I've also attached the code snipped.

Any help is very much appriciated!

Kind regards.



			//leegmaken tabellen

			$result = mysql_query("DELETE FROM temp_grau1",$db);
			$result = mysql_query("DELETE FROM temp_grau2",$db);


   			$handle = fopen("$filename", "r");

    			while (($data = fgetcsv($handle, 10000, ";")) !== FALSE){

			        $import = "INSERT INTO temp_grau1 (CODI,CONCEPTA,TAMANY,PVP,PREU_ESPECIAL,ANYADA,STOCK) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]')";

  				mysql_query($import) or die ("Kan de query1 niet uitvoeren.");



     			print "Import LIJST 1 done";


   			$handle = fopen("$filename", "r");

    			while (($data = fgetcsv($handle, 10000, ";")) !== FALSE){

			        $import = "INSERT INTO temp_grau2 (CODI,CONCEPTA,TAMANY,PVP,PREU_ESPECIAL,ANYADA,STOCK) values('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]','$data[5]','$data[6]')";

  				mysql_query($import) or die ("Kan de query2 niet uitvoeren.");



     			print "Import LIJST 1 done";

			?><META HTTP-EQUIV="refresh" CONTENT="0; url=../Menu_Prijslijst.php"><?php


			print "<br><br><center>Please enter the URL of the files you want to use for the price comparison.";

			print "<form action='Inladen.php' method='post'>";

      			print "Pricelist file 1 (previous file):<br>";

      			print "<input type='file' name='filename1' size='20'><br>";

      			print "Pricelist file 2 (current file):<br>";

      			print "<input type='file' name='filename2' size='20'><br><br>";

     			print "<input type='submit' name='submit' value='COMPARE LISTS'></form><center>";



Open in new window

Question by:Geerd
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 35146640
U could just use PHPMYADMIN


LVL 17

Expert Comment

by:Shinesh Premrajan
ID: 35146642
there could be many reasons
1) Is the table in the server has the enough rights
2) If the table columns names are in lower case

You need to show whats the error message that you got use the following to print the error: mysql_error()

Hope this helps


Author Comment

ID: 35146699
Dear Shinug,

Thank you for the reply. The error message is:

Access denied for user 'XXXXXX'@'localhost' (using password: NO)

I've replaced my name with XXXXXXX in this error message.

Kind regards,

LVL 17

Accepted Solution

Shinesh Premrajan earned 2000 total points
ID: 35146711
you need to change the user that's applicable for the live server, since the test environment user is not a valid user in live.

There is a mysql user that you need to change as per the live server settings.

hope this helps

Author Comment

ID: 35146778
You are right. I don't know why, but I uploaded an old DB connect file ;-)


Thank you !!!

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
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.
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

610 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