php - MySQL - 在 PHP 中批量重命名所有行

标签 php mysql batch-rename

因此,在这段代码中,我刚刚从表中删除了一行,因此,img_pos 的值为: 1,2,3,4,5 现在,它们是(假设我们删除了第三个条目): 1,2,4,5 当然,我希望这样: 1,2,3,4

所以我必须批量重命名行。

我明白了,但似乎不起作用......

$sql = "SELECT * FROM $tableImg WHERE img_acc = $accid ORDER BY img_pos";
$res = mysql_query($sql);
$num = mysql_num_rows($res);

$i = 0;
$n = 1;
while($i<$num){
    $sql = "SELECT * FROM $tableImg WHERE img_acc = $accid ORDER BY img_pos DESC LIMIT $i,1";
    $res = mysql_query($sql);
    $row = mysql_fetch_array($res);
    $img_pos = $row['img_pos'];

    $sql = "UPDATE $tableImg SET img_pos = '$n' WHERE img_acc = '$accid' AND img_pos = '$img_pos'";
    $res = mysql_query($sql);
    $i++;
    $n++;
}
mysql_close();

$tableImg 只是一个包含表名的变量,效果很好。 我猜问题出在“$img_pos = $row['img_pos'];”周围,因为所有查询的使用方式都不同,即使略有不同,但它们应该可以工作......

提前致谢!

最佳答案

我最终只是这样做了:

$i = 1; 
while($row = mysql_fetch_array($res)){
    $old_pos = $row['img_pos'];
    $sql = "UPDATE $tableImg SET img_pos = $i WHERE img_acc = $accid AND img_pos = $old_pos";
    $res = mysql_query($sql);
    $i++;
};

关于php - MySQL - 在 PHP 中批量重命名所有行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17083459/

相关文章:

php - 添加按钮更改 php 中一个字段的功能

php - 如何允许数组中重复的mysql行id?

regex - OSX : change date format in multiple file names

python - 使用 .rename 和 .endswith 重命名多个图像

javascript - 修复源代码中损坏的链接

php - 如何处理像mysql中计算平均分这样的困难过程?

PHP将所有参数作为数组获取?

如果变量为空,MYSQL 忽略 where 子句

php - 为 wordpress 主题和插件创建 satis composer 私有(private)存储库

perl - 如何为目录下的所有文件名添加前缀?