Rien compris de tout ça... Mais je vais chercher...Salut,
Dans ton cas tu peux regarder du côté du replace de mysql mais Perso je m'orienterais vers une procédure stockée à de. Paramètres. Cette procédure fait un sélect count pour savoir si la ligne existe, si oui iodate si non insert.
Je te conseil aussi l'utilisation de pdo et des requêtes préparée.
@+
Heu... n''est-ce pas là tout l'intérêt de la syntaxe : INSERT INTO ... ON DUPLICATE KEY UPDATEJe suppose que tu sais lire le fichier.
Il faut faire une tentative d'update avec les données lues et regarder combien de lignes modifiées - mysql_affected_rows() - et faire l'insert si pas de lignes modifiée.
Une option est de commencer par l'insert et de faire un update en cas d'erreur duplicate key.
CREATE TABLE `furax69` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`numero` int(11) NOT NULL,
`budget` decimal(8,2) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
La procédure Stockée
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `procFurax69`(
IN num INT,
IN bud decimal(10,2),
out ret boolean)
BEGIN
declare nb int;
declare tmp boolean;
set @tmp := false;
select count(*) into nb from furax69 where numero = num ;
if nb = 0 then
insert into furax69 (numero,budget) value(num,bud);
set @tmp := true;
else
update furax69 set budget=bud where numero = num;
set @tmp := true;
end if;
select @tmp into ret;
END$$
DELIMITER ;
Le code php avec PDO
<?php
$fichiercsv = file('furax69.csv');
try {
$pdo = new PDO("mysql:host=localhost;dbname=test;charsey=utf8","root","yyRu2TKEvyYpzFLK");
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $pdo->prepare('call procFurax69(:numero,:budget,@sortie)');
foreach($fichiercsv as $line){
$csv = str_getcsv($line, ';');
/*
$num = 12;
$bud = 45550.52;
*/
$stmt->bindParam(':numero', $csv[0], PDO::PARAM_INT);
$stmt->bindParam(':budget', $csv[1], PDO::PARAM_INT);
//$stmt->bindParam(':sortie', $retour, PDO::PARAM_STR,5);
$stmt->execute();
$s = $pdo->query('select @sortie')->fetch(PDO::FETCH_OBJ);
echo 'La procédure à retourné : ', $s->{'@sortie'};
}
}
catch (PDOException $e){
echo '<pre style="word-wrap:break-word;">';
echo $e->getMessage();
echo '</pre>';
}
?>
Le fichier de test
Code : Tout sélectionner
123456;45789.8
456789;789.5
7832;486.48
159753;486.58
458652;965.12
546;456.25
456.15;48996.52
Code : Tout sélectionner
mysql> select * from furax69;
+----+--------+----------+
| id | numero | budget |
+----+--------+----------+
| 1 | 123456 | 45789.80 |
| 2 | 456789 | 789.50 |
| 3 | 7832 | 486.48 |
| 4 | 159753 | 486.58 |
| 5 | 458652 | 965.12 |
| 6 | 546 | 456.25 |
| 7 | 456 | 48996.52 |
+----+--------+----------+
7 rows in set (0.00 sec)
$req = "INSERT INTO furax69 (numero,budget)
VALUES ('123456','45789.8')
ON DUPLICATE KEY UPDATE
budget = VALUES(budget)
";