473,498 Members | 1,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

From CSV To MySQL Table

Hi, I've a CSV file with 3 columns

ID - Price1 - Price2

How I would like to update the records of a MySQL tables that has the
same ID of the CSV files.

How Can I do this? Read from CSV and update MySQL table?

Thanks
May 14 '07 #1
5 19067
Hi Valeria,

your idea/question to read from csv and update the table is the right
way.

Open your csv file and read each line using a loop. Check inside your
loop, if an entry in the database exists. if so update it, otherwhise
continue the loop or insert an entry.

See follwing pages for detailed information:
- http://de2.php.net/manual/en/function.fgetcsv.php
- http://de2.php.net/manual/en/ref.mysql.php or http://de2.php.net/manual/en/ref.mysqli.php

An simple example could be:
[snip]
$link = mysql_connect($mysqlHost, $user, $password) or die('Could not
connect: ' . mysql_error());
$handle = fopen("myfile.csv", "r");

// loop content of csv file, using comma as delemiter
while (($data = fgetcsv($handle, 1000, ",")) !== false) {
$id = (int) $data[0];
$price = floatval($data[1]);
$price2 = floatval($data[2]);

$query = 'SELECT id FROM my_table';
if (!$result = mysql_query($query)) {
continue;
}

if ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
// entry exists update
$query = "UPDATE my_table SET price='$price', price2='price2'
WHERE id=$id";
mysql_query($query);
if (mysql_affected_rows() <= 0) {
// no rows where affected by update query
}
} else {
// entry don't exists continue or insert...
}

mysql_free_result($result);
}

fclose($handle);
mysql_close($link);
[/snap]

purcaholic

May 14 '07 #2
your idea/question to read from csv and update the table is the right
way.

Open your csv file and read each line using a loop. Check inside your
loop, if an entry in the database exists. if so update it, otherwhise
continue the loop or insert an entry.
As an alternative, you can define a (temporary) table based on your CSV
file in MySQL (you'll have to upload the csv file to the database
server), and use an update statement. MySQL has a CSV engine for these
purposes. If this is a regular action and not a one-time only action,
you'll be better of using PHP though.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
May 14 '07 #3
In article <46***********************@news.xs4all.nl>, Willem Bogaerts
wrote:
If this is a regular action and not a one-time only action,
you'll be better of using PHP though.
Why so? I would have thought it would be the reverse.
Surely such a simple (looking) update as this would be quicker and more
efficient joining tables, even temporary ones, than in php?

Regards
Mark

May 14 '07 #4
Why so? I would have thought it would be the reverse.
Surely such a simple (looking) update as this would be quicker and more
efficient joining tables, even temporary ones, than in php?
If you would do this from a CRON job or a PHP page, it needs the rights
to create a table. That is something I like to avoid. A one-time action
as an administrator is less dangerous, as you don't "run unattended"
yourself.

Best regards,
--
Willem Bogaerts

Application smith
Kratz B.V.
http://www.kratz.nl/
May 14 '07 #5
In article <46*********************@news.xs4all.nl>, Willem Bogaerts wrote:
If you would do this from a CRON job or a PHP page, it needs the rights
to create a table. That is something I like to avoid. A one-time action
as an administrator is less dangerous, as you don't "run unattended"
yourself.
Ah, I see. Thank you for the explanation.

Regards
Mark

May 15 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
5844
by: Dave | last post by:
I have a MS sql 2000 db that needs to sync with a Mysql db. When a password is changed in the MS SQL table, I need to make sure that the same value is updated in the Mysql db. I dont have any...
2
3229
by: Raffi | last post by:
I use the --all-databases switch to backup my entire database. Sometimes there's a need to restore individual databases or tables form the backup file. What command should I use for this? ...
34
5001
by: Karam Chand | last post by:
Hello I have been working with Access and MySQL for pretty long time. Very simple and able to perform their jobs. I dont need to start a flame anymore :) I have to work with PGSQL for my...
48
3812
by: phillip.s.powell | last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't ask) I have database Spring with table Students I have database Summer with table Students I am tasked to produce a...
1
2815
by: Good Man | last post by:
Hi there I've noticed some very weird things happening with my current MySQL setup on my XP Laptop, a development machine. For a while, I have been trying to get the MySQL cache to work....
25
2213
by: frizzle | last post by:
Hi there, I have a mySQL system with a news publishing part in it: Admins can create new items with text in it, and they have an option to create 'fulltexts', so you'd get "read more ..." on the...
15
4563
by: Cheryl Langdon | last post by:
Hello everyone, This is my first attempt at getting help in this manner. Please forgive me if this is an inappropriate request. I suddenly find myself in urgent need of instruction on how to...
2
2143
by: bwana.mpoa | last post by:
Hi, We're using a mySQL database as a replica of another (Sybase) DB for reporting purposes. The Sybase is part of a real-time mission critical system - hence the separate database where people...
7
4174
by: Randy | last post by:
Folks: We have a web-based app that's _really_ slowing down because multiple clients are writing their own private data into a single, central database. I guess the previous programmer did...
2
3874
by: amit2781 | last post by:
Hi, I have created 4 tables in 'amit' database and then I deleted them. Still I able to get information about the table_schema for the table deleted. After drop table when I fire a query for...
0
7126
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6891
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7381
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5465
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4916
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3096
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3087
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1424
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
293
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.