Previously I did many MYSQL queries one after another. So I tried to find a way to make it 1 query to speed up the process.
My old Queries where like:
$con->query("UPDATE `table` SET `slot`=1 WHERE `id`=4563");
$con->query("UPDATE `table` SET `slot`=2 WHERE `id`=263");
$con->query("UPDATE `table` SET `slot`=3 WHERE `id`=1298");
$con->query("UPDATE `table` SET `slot`=4 WHERE `id`=5586");
$con->query("UPDATE `table` SET `slot`=5 WHERE `id`=563");
$con->query("UPDATE `table` SET `slot`=6 WHERE `id`=463");
$con->query("UPDATE `table` SET `slot`=7 WHERE `id`=113");
$con->query("UPDATE `table` SET `slot`=8 WHERE `id`=12");
$con->query("UPDATE `table` SET `slot`=9 WHERE `id`=1385");
[..] (up to 100)
My new idea:
$con->query("UPDATE `table` SET `slot` = (@i := @i + 1) WHERE `id` IN (4563,263,1298,5586,563,463,113,12,1385));
I was expecting that slot
was automatically increasing by 1 for each id
in the ([list])
But somehow it does not execute and also not even giving me an error. What am I missing? And is it even possible what I try to do here? Thanks in advance :D
After reading more into this topic I find out the solution for those who are interested:
$con->query("SET @i=0");
$con->query("UPDATE `table` SET `slot` = (@i := @i + 1) WHERE `id` IN (4563,263,1298,5586,563,463,113,12,1385));
I had to add SET @i=0
to a separate query since mysqli does not allow multiple queries in 1 line. All queries run in s single session so it remembers the previous ran queries such as the one with setting the variable.