<?php
$hostname = "localhost";
$database = "mydatabase";
$username = "root";
$password = "123456";
$connect = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR);
$no = 1;
function change_id($id)
{
global $no;
$sql = 'update my_table set id = ' . $no . ' where id = ' . $id;
mysql_query($sql);
$no = $no + 1;
}
mysql_select_db($database, $connect);
$query_Record = "SELECT id FROM my_table ORDER BY id ASC";
$all_Record = mysql_query($query_Record);
$row_Record = mysql_fetch_assoc($all_Record);
do {
change_id( $row_Record['id'] );
} while ($row_Record = mysql_fetch_assoc($all_Record));
// 重新设置id自增起点
mysql_query('alter table my_bable AUTO_INCREMENT = ' . $no);
echo 'ok';
?>
要是还要更新其他的表,使得更新后的id对应,可在函数change_id中进行各种操作。
相关文章: