|
 |
|
 |
06-22-2004, 11:10 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Jun 2004
Posts: 4
|
csv file to mysql using php
I am using a very common script to insert a csv file
<?
# first get a mysql connection as per the FAQ
$DBhost = "localhost";
$DBuser = "DBuser";
$DBpass = "DBpass";
$DBName = "DBName";
$table = "table";
mysql_connect($DBhost,$DBuser,$DBpass,$DBName) or die("Unable to connect to database");
$fcontents = file ('./topscsv.csv');
# expects the csv file to be in the same dir as this script
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode(",", $line);
#if your data is comma separated
# instead of tab separated,
# change the '\t' above to ','
$sql = "insert into $table values ('".
implode("','", $arr) ."')";
mysql_query($sql);
echo $sql ."<br>\n";
if(mysql_error()) {
echo mysql_error() ."<br>\n";
}
}
?>
It echos the data fine, but every other line says
No Database Selected
I am sure it is something stupid, but I can't by this issue
Thanks for your help
Jayteema
|
|
|
06-22-2004, 11:16 AM
|
#2 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
you need to select a db after you connect: http://www.php.net/manual/en/functio...-select-db.php
PHP Code:
<?php
$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Can\'t use foo : ' . mysql_error());
}
?>
__________________
Mike
|
|
|
06-22-2004, 01:34 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Jun 2004
Posts: 4
|
Thanks for the quick Reply!
|
|
|
06-23-2004, 07:21 AM
|
#4 (permalink)
|
|
Registered User
Join Date: Jun 2004
Posts: 4
|
I am getting a parse error now \\Can anyone see an issue
Thanks for your help
PHP Code:
<?
# first get a mysql connection as per the FAQ
$link = mysql_connect('localhost', 'topselec_jt', 'bucket');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// make foo the current db
$db_selected = mysql_select_db('topselec_data', $link);
if (!$db_selected) {
die ('Can't use foo : ' . mysql_error());
}
$fcontents = file ('./topscsv.csv');
# expects the csv file to be in the same dir as this script
for($i=0; $i<sizeof($fcontents); $i++) {
$line = trim($fcontents[$i]);
$arr = explode(",", $line);
#if your data is comma separated
# instead of tab separated,
# change the 't' above to ','
$sql = "insert into $table values ('".
implode("','", $arr) .")";
mysql_query($sql);
echo $sql ."<br>n";
if(mysql_error()) {
echo mysql_error() ."<br>n";
}
}
?>
Last edited by sde; 06-23-2004 at 10:12 AM.
|
|
|
06-23-2004, 10:13 AM
|
#5 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
when you are getting parse errors, please report the line that the error is happening on. it makes helping you much easier ..
just from looking at it, i say it's your first comment
PHP Code:
# first get a mysql connection as per the FAQ
'#' is not a comment character in php.. use '//'
PHP Code:
// first get a mysql connection as per the FAQ
__________________
Mike
|
|
|
06-23-2004, 10:14 AM
|
#6 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
on, and now i see you have a LOT of those style comments .. change them all to //
__________________
Mike
|
|
|
06-23-2004, 04:17 PM
|
#7 (permalink)
|
|
Senior Grasshopper
Join Date: Jun 2003
Location: FL
Posts: 317
|
I believe # is a valid comment character ( http://www.php.net/manual/en/languag...x.comments.php ), but // seems to be the preferred style for one-liners.
Here's your parse error:
Code:
die ('Can't use foo : ' . mysql_error());
You need to escape the quote in can't .
-r
|
|
|
06-23-2004, 04:24 PM
|
#8 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
cool, i've never seen that before. nice catch.
__________________
Mike
|
|
|
06-23-2004, 05:55 PM
|
#9 (permalink)
|
|
Registered User
Join Date: Feb 2003
Location: Australia
Posts: 46
|
I noticed the parse error just with this boards PHP code highlighting. The 2nd half of the code is all red - that's not right.
|
|
|
07-02-2004, 10:58 AM
|
#10 (permalink)
|
|
Registered User
Join Date: Jun 2004
Posts: 4
|
How do You turn on this boards PHP code highlighting
Any help would be appreciated on how to see what JeC is looking at?
Thanks
Jim
|
|
|
07-02-2004, 11:20 AM
|
#11 (permalink)
|
|
Moderator
Join Date: May 2002
Location: us.ca
Posts: 4,444
|
he's just referring to the highlighting.. not really anything to do with your script. ( i think )
this board is just using the built in php highlighting function. nothing custom really.
__________________
Mike
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 06:42 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|