Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 06-22-2004, 11:10 AM   #1 (permalink)
jayteema
Registered User
 
Join Date: Jun 2004
Posts: 4
jayteema is on a distinguished road
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
jayteema is offline   Reply With Quote
Old 06-22-2004, 11:16 AM   #2 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 06-22-2004, 01:34 PM   #3 (permalink)
jayteema
Registered User
 
Join Date: Jun 2004
Posts: 4
jayteema is on a distinguished road
Thanks for the quick Reply!
jayteema is offline   Reply With Quote
Old 06-23-2004, 07:21 AM   #4 (permalink)
jayteema
Registered User
 
Join Date: Jun 2004
Posts: 4
jayteema is on a distinguished road
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'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.
jayteema is offline   Reply With Quote
Old 06-23-2004, 10:13 AM   #5 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Old 06-23-2004, 10:14 AM   #6 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
on, and now i see you have a LOT of those style comments .. change them all to //
__________________
Mike
sde is offline   Reply With Quote
Old 06-23-2004, 04:17 PM   #7 (permalink)
idx
Senior Grasshopper
 
idx's Avatar
 
Join Date: Jun 2003
Location: FL
Posts: 317
idx is on a distinguished road
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
idx is offline   Reply With Quote
Old 06-23-2004, 04:24 PM   #8 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
cool, i've never seen that before. nice catch.
__________________
Mike
sde is offline   Reply With Quote
Old 06-23-2004, 05:55 PM   #9 (permalink)
JeC
Registered User
 
JeC's Avatar
 
Join Date: Feb 2003
Location: Australia
Posts: 46
JeC is on a distinguished road
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.
JeC is offline   Reply With Quote
Old 07-02-2004, 10:58 AM   #10 (permalink)
jayteema
Registered User
 
Join Date: Jun 2004
Posts: 4
jayteema is on a distinguished road
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
jayteema is offline   Reply With Quote
Old 07-02-2004, 11:20 AM   #11 (permalink)
sde
Moderator
 
sde's Avatar
 
Join Date: May 2002
Location: us.ca
Posts: 4,444
sde is on a distinguished road
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
sde is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
cant connect to mysql databases using php eran PHP 11 08-07-2004 08:02 AM
does PHP talk to JAVA class file?? Keith PHP 2 06-18-2004 08:23 AM
Help with setting up mySQL and PHP Ilya020 PHP 11 03-19-2003 05:10 AM


All times are GMT -8. The time now is 06:42 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting