Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Importing .csv Into Mysql Database, NEED HELP!
Mystixs
post Jan 27 2007, 05:31 AM
Post #1


Member [Level 1]
****

Group: Members
Posts: 57
Joined: 23-January 07
Member No.: 37,683



I need help importing a .csv contents into a Mysql database.
I have this. But its not working.

CODE

<?php
connection = mysql_connect("******", "**********", "********") or die ("Unable
to connect to server");
$db = mysql_select_db("b9_259782_CC", $connection) or die ("Unable to
select database");

fopen ('csvranks.csv', 'r');
mysql_query("INSERT INTO test_table (id, name, guild, level, exp)
?>


What am I doing wrong? The mysql table is all set up..

Any help is GREATLY appreciated. If you need more info just ask.

Thanks!

EDIT: The CSV file looks like this one http://movoda.net/api/csvranks.html

Notice from BuffaloHELP:
Topic modified


This post has been edited by BuffaloHELP: Jan 27 2007, 09:10 PM
Go to the top of the page
 
+Quote Post
Kubi
post Jan 27 2007, 05:40 AM
Post #2


To Cool for Cache
Group Icon

Group: [MODERATOR]
Posts: 1,091
Joined: 16-June 05
From: Some Place.
Member No.: 8,317
T17 GFX Crew



Would you mind showing us the error page? It could be an incorrect db nname/password
Go to the top of the page
 
+Quote Post
master_bacarra
post Jan 27 2007, 06:24 AM
Post #3


I'm back... well, sort of.
*********

Group: [HOSTED]
Posts: 697
Joined: 26-December 05
From: somewhere in the middle of nowhere
Member No.: 16,226
Spam Patrol



i told you, you haven't looked enough through the search engines tongue.gif

you opened the file, but you didn't use a handle for it.

try this one

CODE

$file_handle = fopen ("filename.csv");

while (($row = fgetcsv($file_handle)) !== false) {
  $data = explode (",",$row);
  $insertrecord =
        "INSER INTO `$tablename` VALUES ('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."','".$data[4]."')";
   mysql_query($insterrecord);
}

fclose (file_handle);


i've adjusted the code to fit the csv file you have indicated.

of course that is assumed that all of the values in each row are string. your database could be indicating some of the attributes as of integer type. convert data[3] and data[4] to integer values (i'm assuming that you're keeping the leading zeros in the values of data[0]). i think the function is intval() taking the string as an argument. so the line

CODE

  $insertrecord =
        "INSER INTO `$tablename` VALUES ('".$data[0]."','".$data[1]."','".$data[2]."','".$data[3]."','".$data[4]."')";


will become

CODE

  $insertrecord =
        "INSER INTO `$tablename` VALUES ('".$data[0]."','".$data[1]."','".$data[2]."','".intval($data[3])."','".intval($data[4])."')";



also, change the '$tablename' to the name of the table where these values are to be inserted.
hope that helps (and i hope that works).

This post has been edited by master_bacarra: Jan 27 2007, 06:27 AM
Go to the top of the page
 
+Quote Post
Mystixs
post Jan 27 2007, 02:43 PM
Post #4


Member [Level 1]
****

Group: Members
Posts: 57
Joined: 23-January 07
Member No.: 37,683



Ok. First, KuBi. I didn't get any error.. It connected to the database, and selected the database. It was just a blank page...

Anyway, master_bacarra thanks, I will try this. I made need more help though. I'm not very good at PHP and Mysql..
Go to the top of the page
 
+Quote Post
Mystixs
post Jan 27 2007, 02:58 PM
Post #5


Member [Level 1]
****

Group: Members
Posts: 57
Joined: 23-January 07
Member No.: 37,683



I guess this is because my lack of knowledge and slow learning but i'm having trouble..
I will show what I have now.

The Mysql table:
CODE

ALTER TABLE `test_table` CHANGE `id` `id` INT( 100 ) NOT NULL DEFAULT '0',
CHANGE `name` `name` INT( 100 ) NOT NULL DEFAULT '0',
CHANGE `guild` `guild` INT( 100 ) NOT NULL DEFAULT '0',
CHANGE `level` `level` INT( 100 ) NOT NULL DEFAULT '0',
CHANGE `exp` `exp` INT( 100 ) NOT NULL DEFAULT '0'


The PHP:
CODE


<?php
connection = mysql_connect("sql1.byethost9.com", "***", "***") or die ("Unable
to connect to server");
$db = mysql_select_db("b9_259782_CC", $connection) or die ("Unable to
select database");

$file_handle = fopen ("csvranks.csv");

while (($row = fgetcsv($file_handle)) !== false) {
  $data = explode (",",$row);
    $insertrecord =
        "INSER INTO `tst_table` VALUES ('".$data[0]."','".$data[1]."','".$data[2]."','".intval($data[3])."','".intval($data[4])."')";
   mysql_query($insterrecord);
}

fclose (file_handle);
?>




http://chesspieces.byethost9.com/test.php thats the page its saved to.

I go to it and it does nothing. I check the database table and its empty..

Sorry for the bother..

Go to the top of the page
 
+Quote Post
master_bacarra
post Jan 27 2007, 09:49 PM
Post #6


I'm back... well, sort of.
*********

Group: [HOSTED]
Posts: 697
Joined: 26-December 05
From: somewhere in the middle of nowhere
Member No.: 16,226
Spam Patrol



QUOTE(Mystixs @ Jan 27 2007, 10:58 PM) *

The Mysql table:
CODE

ALTER TABLE `test_table` CHANGE `id` `id` INT( 100 ) NOT NULL DEFAULT '0',
CHANGE `name` `name` INT( 100 ) NOT NULL DEFAULT '0',
CHANGE `guild` `guild` INT( 100 ) NOT NULL DEFAULT '0',
CHANGE `level` `level` INT( 100 ) NOT NULL DEFAULT '0',
CHANGE `exp` `exp` INT( 100 ) NOT NULL DEFAULT '0'


i don't get it. you've given me the database entries, but two attributes don't say that it is of INT data type. the id attribute can be of INT, same goes with level and exp. but i don't get it why you're putting name and guild attributes to INT. it doesn't match your table entries indicated in the csv file.

here's one sample data:
0000000073,moc1,CoM,26,594169

assuming that i'm thinking that each entry is under the correct attribute, then 0000000073 is the id, moc1 is the name, CoM is the guild, 26 is the level, and 594169 is the exp. you can't put name and guild as INT because it won't match moc1 and CoM. it won't match the data types.

like what KuBi has said, i think you should post the error here.


QUOTE(Mystixs @ Jan 27 2007, 10:58 PM) *

The PHP:
CODE


<?php
connection = mysql_connect("sql1.byethost9.com", "***", "***") or die ("Unable
to connect to server");
$db = mysql_select_db("b9_259782_CC", $connection) or die ("Unable to
select database");

$file_handle = fopen ("csvranks.csv");

while (($row = fgetcsv($file_handle)) !== false) {
  $data = explode (",",$row);
    $insertrecord =
        "INSER INTO `tst_table` VALUES ('".$data[0]."','".$data[1]."','".$data[2]."','".intval($data[3])."','".intval($data[4])."')";
   mysql_query($insterrecord);
}

fclose (file_handle);
?>




don't forget to put $ before the php variables you're using. put $ before connection and file_handle (the one inside fclose()). query should be INSERT not INSER.
Go to the top of the page
 
+Quote Post
Mystixs
post Jan 27 2007, 10:06 PM
Post #7


Member [Level 1]
****

Group: Members
Posts: 57
Joined: 23-January 07
Member No.: 37,683



Thanks. Problems resolved smile.gif
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Complete Login System(56)
  2. Learning Mysql(3)
  3. Import From Excel File Into Mysql Database(7)
  4. Mysql Database Size(6)
  5. Connect To Remote Oracle Database With Toad(6)
  6. Can You Add Images Into A Mysql Database?(20)
  7. Check Referrer To Prevent Linking Yours From Other Sites(8)
  8. Is It Possible To Access The Mysql Remotely?(10)
  9. Qupis : Free Hosting With Php, Mysql, Cpanel. (one Line Text Ad At Bottom)(41)
  10. Mysql Won't Update(4)
  11. Mysql + Php Question(5)
  12. Tools Needed!(9)
  13. Database With Mysql++(7)
  14. Mysql-essential-5.0.51 Installion Problem(2)
  15. Best Php And Mysql Editor For Noobs(6)
  1. I Need Help Transferring Sql Database?(3)
  2. Html Form!(4)
  3. How To Manage The Database For E-books Website In My Sql?(3)
  4. Mysql(2)
  5. Mysql Error(3)
  6. Create Table - Mysql Code - Help(1)
  7. Phpmyadmin(6)
  8. Remote Access Mysql(8)
  9. Please Explain Me Mysql(4)
  10. How Do I Find My Mysql Password? [resolved](5)
  11. Database Or Pdf(1)
  12. Is The Database System Gone Again? [resolved](12)
  13. Need Help With My Database(5)