Jump to content
xisto Community
Mystixs

Importing .csv Into Mysql Database NEED HELP

Recommended Posts

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

<?phpconnection = mysql_connect("******", "**********", "********") or die ("Unableto connect to server");$db = mysql_select_db("b9_259782_CC", $connection) or die ("Unable toselect 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

Edited by BuffaloHELP (see edit history)

Share this post


Link to post
Share on other sites

Would you mind showing us the error page? It could be an incorrect db nname/password

Share this post


Link to post
Share on other sites

i told you, you haven't looked enough through the search engines ;)

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

try this one

$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

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

will become

  $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).
Edited by master_bacarra (see edit history)

Share this post


Link to post
Share on other sites

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..

Share this post


Link to post
Share on other sites

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:

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:
<?phpconnection = mysql_connect("sql1.byethost9.com", "***", "***") or die ("Unableto connect to server");$db = mysql_select_db("b9_259782_CC", $connection) or die ("Unable toselect 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);?>


/legacy 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..

Share this post


Link to post
Share on other sites

The Mysql table:

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.


The PHP:

<?phpconnection = mysql_connect("sql1.byethost9.com", "***", "***") or die ("Unableto connect to server");$db = mysql_select_db("b9_259782_CC", $connection) or die ("Unable toselect 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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.