Ver Mensaje Individual
  #2 (permalink)  
Antiguo 18/10/2009, 07:50
Avatar de gjx2
gjx2
 
Fecha de Ingreso: agosto-2008
Ubicación: R.D
Mensajes: 1.153
Antigüedad: 15 años, 8 meses
Puntos: 139
Respuesta: renumerar un contador de registros en mysql

Ejemplo


Código SQL:
Ver original
  1. CREATE TABLE  `prueba` (
  2.   `Codigo` INT(11) NOT NULL AUTO_INCREMENT,
  3.   PRIMARY KEY  (`Codigo`)
  4. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;


Código PHP:
Ver original
  1. include("conectar.php");
  2.  
  3. $sql = mysql_query("select * from prueba");
  4. $b = 1;
  5. while( $field = mysql_fetch_array($sql) ){
  6.  
  7.     if($field["Codigo"] != $b){
  8.    
  9.         mysql_query("UPDATE prueba set Codigo=$b where  codigo=$field[Codigo]");
  10.     }
  11.    
  12.     $b++;
  13.    
  14.  
  15. }