943,808 Members | Top Members by Rank

Ad:
  • MySQL Discussion Thread
  • Marked Solved
  • Views: 16570
  • MySQL RSS
Aug 21st, 2008
0

Update the auto increment value

Expand Post »
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!

MySQL Syntax (Toggle Plain Text)
  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. ?>
Similar Threads
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
Aug 22nd, 2008
0

Re: Update the auto increment value

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.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Aug 22nd, 2008
0

Re: Update the auto increment value

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

MySQL Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
Aug 22nd, 2008
0

Re: Update the auto increment value

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.
Reputation Points: 12
Solved Threads: 3
Newbie Poster
mike_g is offline Offline
14 posts
since Jul 2008
Aug 23rd, 2008
0

Re: Update the auto increment value

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.
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008
Dec 15th, 2008
0

Re: Update the auto increment value

php Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 7
Junior Poster in Training
danishbacker is offline Offline
97 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MySQL Forum Timeline: qick question
Next Thread in MySQL Forum Timeline: How select Documents which contain more than n tag-specified words





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC