0

Is it possible to update the AUTO_INCREMENT value who's attributes are primary key, not null, auto increment and also an INT type

The thing is I need to synchronize the 'image_id' to access images sequentially
and if one row is deleted the sequence is destroyed Help me!

<?php
include('db_conexn.php');
$data = mysql_query("SELECT image_id FROM imagetable")
or die(mysql_error()); 
while($info = mysql_fetch_array( $data )) {
	echo $id += 1;
	echo " - ".$info['image_id']; 
	$sync=mysql_query("UPDATE imagetable SET image_id = $id");
	echo "<BR>";
}
echo " - <BR> " . $id +=1;
$autoinc = mysql_query("ALTER TABLE imagetable AUTO_INCREMENT = $id");
?>
3
Contributors
5
Replies
10
Views
9 Years
Discussion Span
Last Post by danishbacker
0

I would suggest it is bad practice to use the image_id for sequence. Add an int column to use for sequence, update it if necessary.
You shouldn't need to update it, if you use order by with sequence column, this would be a much cleaner way, than updating an id column.

0

if a row is deleted the sequence will be destroyed can you suggest me any way to update the id.
i've done using following code

$id=1;
$count = mysql_num_rows( mysql_query("SELECT image_id FROM imagetable") )
$data = mysql_query("SELECT * FROM imagetable ORDER BY image_path ASC")
or die(mysql_error());
while($info = mysql_fetch_array($data)) {
$sync = mysql_query("UPDATE imagetable SET image_id = $id ");
}

this is not working ie all ids have same number
it all contains the last value if $id
help me

0

It seems that you are not increment the id maybe add an increment to your loop? IE:

$id=1;
$count = mysql_num_rows( mysql_query("SELECT image_id FROM imagetable") )
$data = mysql_query("SELECT * FROM imagetable ORDER BY image_path ASC")
or die(mysql_error());
while($info = mysql_fetch_array($data)) 
{
    $sync = mysql_query("UPDATE imagetable SET image_id = $id ");
    $id++;
}
0

i have updated $id but still the problem persists
the loop is execute completely on a row and only after that it goes to the next row
this is why every row has same number
help me solve this

0
$data = mysql_query("SELECT id ,name FROM imagetable")
or die(mysql_error()); 
$id=0;
while($info = mysql_fetch_array( $data )) {
$id=$id+1;
mysql_query("UPDATE imagetable SET id =$id  where name='$info[1]'");
}

instead of using auto increment i incremented the value manually and
updated each fields.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.