Server Administration
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesServer Administration

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
You eat, breathe and sleep innovation. Build your mobile intelligence with BlackBerry® experts this July. Register Today!
  #1  
Old August 31st, 2006, 11:35 PM
hopeless_ninja hopeless_ninja is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 10 hopeless_ninja User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Importing CSV into DB

Ok, so I have spent a lot of time here today trying to get the update right. The issue that I am having is that the following code just grabs one value from the CSV file:

php Code:
Original - php Code
  1.  
  2. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
  3.            $num = count($data);
  4.            $row++;
  5.            for ($counter=$columnheadings; $counter < $num; $counter++) {
  6.               
  7.                echo $data[$counter] . "<br />n";
  8.                $insertrecord = "Insert Into `$tablename` Values ($data[$counter])";
  9.            mysql_query($insertrecord)


So I changed things around to format a SQL statement that will run. The problem is that I cannot get the string replace function to remove the last comma. I know the fix is probably simple, but I guess I am tired and out of ideas. Or maybe there is a better way to do this. In any case, I appreciate the help. Here's the current code:

php Code:
Original - php Code
  1.  
  2. $handle = fopen("test.csv", "r");
  3. while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
  4.    $num = count($data);
  5.    echo "<p> $num fields in line $row: <br /></p>n";
  6.    $insertrecord = "Insert Into `$tablename` Values (";
  7.    $row++;
  8.    for ($c=0; $c < $num; $c++) {
  9.        $insertrecord .= "'$data[$c]',";
  10.    }
  11.    $insertrecord .= ")";
  12.    echo $insertrecord;
  13.    str_replace(",)", ")", $insertrecord)

Reply With Quote
  #2  
Old August 31st, 2006, 11:36 PM
hopeless_ninja hopeless_ninja is offline
Codewalkers Newbie (0 - 499 posts)
 
Join Date: Apr 2007
Posts: 10 hopeless_ninja User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
RE: Importing CSV into DB

By the way, this is the formatted SQL:
Insert Into `playground` Values ('1-FGFK5','SPR46',)

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesServer Administration > Importing CSV into DB


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway