Populating a MySQL table with data from a csv file

Thread Solved

Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Populating a MySQL table with data from a csv file

 
0
  #1
Feb 11th, 2008
Hi everyone

I have a csv file with data such as first name, surname, tel etc and I'd like to populate a table inside my MySQL database automatically.

So far I have this but I keep getting an access denied error and I don't know why:
  1. <?php
  2.  
  3. $server = 'localhost';
  4. $user = 'username';
  5. $password = 'password';
  6.  
  7. // open connection to MySQL server
  8. $connection = mysql_connect($server,$user,$password)
  9. or die ('Unable to connect!');
  10.  
  11. //select database
  12. mysql_select_db('table_name') or die ('Unable to select database!');
  13.  
  14. // clear the table down
  15. $sql = "TRUNCATE TABLE table_name";
  16.  
  17. // run the first query to clear table
  18. mysql_query($sql) or die(mysql_error());
  19.  
  20. // set up query to import data, assuming the
  21. // csv file is at the top level of the C drive
  22. $sql = "LOAD DATA INFILE 'users.txt'
  23. INTO TABLE table_name
  24. FIELDS TERMINATED BY ','
  25. OPTIONALLY ENCLOSED BY '\"'
  26. LINES TERMINATED BY '\r\n'";
  27.  
  28. // run the query to load the data
  29. mysql_query($sql) or die(mysql_error());
  30.  
  31. ?>

Any help would be greatly appreciated. Virtual cookies are up for grabs as a reward
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Populating a MySQL table with data from a csv file

 
0
  #2
Feb 11th, 2008
Your code works fine on my computer. You are probably getting 'access denied' because you don't have enough privileges for the mysql user. Use grant to grant permissions for the users.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Populating a MySQL table with data from a csv file

 
0
  #3
Feb 11th, 2008
Originally Posted by nav33n View Post
Your code works fine on my computer. You are probably getting 'access denied' because you don't have enough privileges for the mysql user. Use grant to grant permissions for the users.
Ok, I'll give that a try and let you know.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Populating a MySQL table with data from a csv file

 
0
  #4
Feb 11th, 2008
Oh, btw, users.txt should be in mysql/data/dbname/ ! That's where it looks.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Populating a MySQL table with data from a csv file

 
0
  #5
Feb 11th, 2008
Ok, it can't be grant access because I'm using administrator. I thought it might be my windows firewall preventing access but I've turned it off and I'm still getting the access denied error. Are there any other possibilities?

I've tried this on 2 separate servers not associated with one another at all and I'm getting the same access denied error.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Populating a MySQL table with data from a csv file

 
0
  #6
Feb 11th, 2008
Originally Posted by nav33n View Post
Oh, btw, users.txt should be in mysql/data/dbname/ ! That's where it looks.
Ok, but that won't cause the access error would it?
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Populating a MySQL table with data from a csv file

 
0
  #7
Feb 11th, 2008
Originally Posted by Venom Rush View Post
Ok, it can't be grant access because I'm using administrator. I thought it might be my windows firewall preventing access but I've turned it off and I'm still getting the access denied error. Are there any other possibilities?

I've tried this on 2 separate servers not associated with one another at all and I'm getting the same access denied error.
No. I am talking about mysql user. Not the one you use to log on to windows.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Populating a MySQL table with data from a csv file

 
0
  #8
Feb 11th, 2008
Originally Posted by Venom Rush View Post
Ok, but that won't cause the access error would it?
Nope. If it's outside that directory, it will generate an error with errorcode 2.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 260
Reputation: Venom Rush is an unknown quantity at this point 
Solved Threads: 2
Venom Rush's Avatar
Venom Rush Venom Rush is offline Offline
Posting Whiz in Training

Re: Populating a MySQL table with data from a csv file

 
0
  #9
Feb 11th, 2008
Originally Posted by nav33n View Post
No. I am talking about mysql user. Not the one you use to log on to windows.
What I meant was I'm using the MySQL root user.
This user has a spatula. We don't know why, but we are afraid.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 3,760
Reputation: nav33n is a jewel in the rough nav33n is a jewel in the rough nav33n is a jewel in the rough 
Solved Threads: 332
Moderator
Featured Poster
nav33n's Avatar
nav33n nav33n is offline Offline
Senior Poster

Re: Populating a MySQL table with data from a csv file

 
0
  #10
Feb 11th, 2008
Hmm.. user root has all the permissions AFAIK. But if you have access to phpmyadmin, go to my sql database and select table user. Check the permission option under File_priv. If its N, make it Y.
Hope that helps.
Ignorance is definitely not bliss!

*PM asking for help will be ignored*
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC