|
Posted by purcaholic on 05/14/07 09:45
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
Navigation:
[Reply to this message]
|