Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I set an id column in PHP

Tags:

php

mysql

I am able to set a id column in my table, using this sql code in phpmyadmin:

SET @i = 0;
UPDATE albums SET id=(@i:=@i+1);

this wil give each row in the id column a consecutive number. it might me very basic, but how do I run this code in PHP?

I tried:

mysql_query('SET @i = 0;
UPDATE albums SET id=(@i:=@i+1);');

but that doesn't do anything

Thanks for the advice! best Sam

like image 380
Sam Avatar asked Mar 15 '26 21:03

Sam


1 Answers

mysql_query() sends a unique query (multiple queries are not supported),So that your code not works.You can try it with different mysql_query().

mysql_query('SET @i = 0');
mysql_query('UPDATE albums SET id=(@i:=@i+1)');
like image 161
Shijin TR Avatar answered Mar 17 '26 12:03

Shijin TR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!