1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
| <?php
$serveur = "localhost";
$user = "root";
$pwd = "";
$bd = "test";
$conx = mysql_connect($serveur,$user,$pwd);
mysql_select_db($bd,$conx);
set_time_limit(0);
// Liste les tables
$listtable = mysql_list_tables($bd);
while ($ntable=mysql_fetch_array($listtable))
{
$nomtable= $ntable[0];
$sql = "SELECT * FROM `$nomtable`";
$req = mysql_query($sql);
// Liste de tous les champs
$i=0;
$pkey = "";
$chp ="";
while ($i < mysql_num_fields($req)) {
$resf=mysql_fetch_field($req,$i);
// Liste de toutes les clés primaires et des champs non numériques
if ($resf->primary_key==1){
if (!empty($pkey))
$pkey .= "|";
$pkey .= $resf->name ;
}
if ($resf->numeric==0){
if (!empty($chp))
$chp .= "|";
$chp .= $resf->name ;
}
$i++;
}
if ((!empty($chp)) && (!empty($pkey))){
// Création de la requete de modification
$clepri = explode("|",$pkey);
$chp2=explode("|",$chp);
while($res=mysql_fetch_array($req)){
$reqU="UPDATE `$nomtable` SET ";
$reqW= "`$clepri[0]`='" . $res[$clepri[0]] . "'";
if (count($clepri)>1){
$nbrechp= count($clepri);
for ($i=1;$i<$nbrechp;$i++)
$reqW.= ",`$clepri[$i]`='" . $res[$clepri[$i]] . "'";
}
$reqS = "`$chp2[0]`='" . htmlentities($res[$chp2[0]], ENT_QUOTES) . "'";
if (count($chp2)>1){
$nbrechp= count($chp2);
for ($i=1;$i<$nbrechp;$i++)
$reqS.= ",`$chp2[$i]`='" . htmlentities($res[$chp2[$i]], ENT_QUOTES) . "'";
}
$reqU .= $reqS . " WHERE " . $reqW;
mysql_query($reqU);
}
}
echo $nomtable . " fait<br />";
}
?> |
Partager