Update the auto increment value

Thread Solved

Join Date: Apr 2008
Posts: 75
Reputation: danishbacker is an unknown quantity at this point 
Solved Threads: 5
danishbacker's Avatar
danishbacker danishbacker is offline Offline
Junior Poster in Training

Update the auto increment value

 
0
  #1
Aug 21st, 2008
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!

  1. <?php
  2. include('db_conexn.php');
  3. $data = mysql_query("SELECT image_id FROM imagetable")
  4. OR die(mysql_error());
  5. while($info = mysql_fetch_array( $data )) {
  6. echo $id += 1;
  7. echo " - ".$info['image_id'];
  8. $sync=mysql_query("UPDATE imagetable SET image_id = $id");
  9. echo "<BR>";
  10. }
  11. echo " - <BR> " . $id +=1;
  12. $autoinc = mysql_query("ALTER TABLE imagetable AUTO_INCREMENT = $id");
  13. ?>
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,162
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: Update the auto increment value

 
0
  #2
Aug 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: danishbacker is an unknown quantity at this point 
Solved Threads: 5
danishbacker's Avatar
danishbacker danishbacker is offline Offline
Junior Poster in Training

Re: Update the auto increment value

 
0
  #3
Aug 22nd, 2008
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

  1. $id=1;
  2. $count = mysql_num_rows( mysql_query("SELECT image_id FROM imagetable") )
  3. $data = mysql_query("SELECT * FROM imagetable ORDER BY image_path ASC")
  4. OR die(mysql_error());
  5. while($info = mysql_fetch_array($data)) {
  6. $sync = mysql_query("UPDATE imagetable SET image_id = $id ");
  7. }

this is not working ie all ids have same number
it all contains the last value if $id
help me
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 14
Reputation: mike_g is an unknown quantity at this point 
Solved Threads: 3
mike_g mike_g is offline Offline
Newbie Poster

Re: Update the auto increment value

 
0
  #4
Aug 22nd, 2008
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++;
}
Last edited by mike_g; Aug 22nd, 2008 at 5:59 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: danishbacker is an unknown quantity at this point 
Solved Threads: 5
danishbacker's Avatar
danishbacker danishbacker is offline Offline
Junior Poster in Training

Re: Update the auto increment value

 
0
  #5
Aug 23rd, 2008
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
Last edited by danishbacker; Aug 23rd, 2008 at 4:59 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 75
Reputation: danishbacker is an unknown quantity at this point 
Solved Threads: 5
danishbacker's Avatar
danishbacker danishbacker is offline Offline
Junior Poster in Training

Re: Update the auto increment value

 
0
  #6
Dec 15th, 2008
  1. $data = mysql_query("SELECT id ,name FROM imagetable")
  2. or die(mysql_error());
  3. $id=0;
  4. while($info = mysql_fetch_array( $data )) {
  5. $id=$id+1;
  6. mysql_query("UPDATE imagetable SET id =$id where name='$info[1]'");
  7. }

instead of using auto increment i incremented the value manually and
updated each fields.
Last edited by danishbacker; Dec 15th, 2008 at 5:01 am.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC