<?php /** Détruit les \ en trop dans un champs de table SQL du à ce salopard de magic quote. Utiliser en ligne de commande. ATTENTION: Si il y a dans ce champs des \ "légaux", il seront virés quand meme. Delete the \ in a field in a SQL table due to stupid magic quote. User in command line. WARNING: If there are \ in the field that meant to be there, they'll be killed anyway. Salagir Fev 2008 - Feb 2008 v1.1 Oct 2008 GPL */ define('DEBUG', false); // http://blog.salagir.com/fr/28-la-1468456e-classe-php-qui-surcharge-mysql/ require_once('./sqlQueries.class.php'); $SQL = new sqlQueries($YOUR_DATABASE, $YOUR_LOGIN, $YOUR_PASS); if (!$argv[1]) { echo "Usage: php del_slashs.php [nomtable] [nomchamp]\n"; } $Fields = $SQL->query2assoc("SHOW FIELDS FROM ".$SQL->e($argv[1])); if (!$Fields) die("Table '$argv[1]' non reconnue.\n"); $index = array(); $found = false; $champs = array(); foreach($Fields as $v) { if ($v['Key']=='PRI') $index[] = $v['Field']; if ($v['Field']==$argv[2]) $found=true; $champs[] = $v['Field']; } $champs = implode(', ', $champs); if (!$found) die("Champs '$argv[2]' non trouvé dans la table.\nChamps dispos: $champs.\n"); if (!$index) die("Je n'ai pas trouve de clef primaire.\n"); echo "On va modifier tous les '$argv[2]' de la table '$argv[1]', avec pour clef '".implode('|',$index)."'.\n"; echo "Faites Ctrl+c si ce n'est pas ce que vous voulez.\n"; sleep(5); $argv[1] = $SQL->e($argv[1]); $argv[2] = $SQL->e($argv[2]); $R = $SQL->query("SELECT `$argv[2]`, `".implode('`, `', $index)."` FROM $argv[1]"); while($r = mysql_fetch_assoc($R)) { $v = str_replace('\\', '', $r[$argv[2]]); if ($v==$r[$argv[2]]) continue; $v = $SQL->e($v); $Q = "UPDATE $argv[1] SET $argv[2]='$v' WHERE ".fillIndex($index, $r)." LIMIT 1"; if (DEBUG!==true) $SQL->query($Q); else echo "$Q\n"; } echo "Terminé.\n"; function fillIndex($champs, $valeurs) { global $SQL; $conds = array(); foreach($champs as $c) { $V = $SQL->e($valeurs[$c]); $conds[] = "`$c`='$V'"; } return implode (' AND ', $conds); } ?>