Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:44:29+00:00 2026-06-07T03:44:29+00:00

Problem: Primary key field is ‘ID’ Data is inserted/updated into it using a REPLACE

  • 0

Problem:

Primary key field is 'ID'

Data is inserted/updated into it using a REPLACE INTO command, which is easy to use but unfortunately increases the 'ID' value of the Record it is replacing.

So I need a way to completely rebuild the ID feild so that:

| ID  |  Name   |
|===============
| 21  |  deer   |
| 8   |  snow   |
| 3   |  tracks |
| 14  |  arrow  |

Goes to:

| ID |  Name   |
|===============
| 1  |  deer   |
| 2  |  snow   |
| 3  |  tracks |
| 4  |  arrow  |

and I need to do it through php.

Current Attempts:

<?php
$reset = "SET @num := 0;
UPDATE `users` SET `ID` = @num := (@num+1);
ALTER TABLE `users` AUTO_INCREMENT =1;";

$con = mysql_connect("mysql2.000webhost.com","db_user","password");  
if (!$con)
{
     die('Could not connect: ' . mysql_error());
}
mysql_select_db("db_name", $con);
if (!mysql_query($reset,$con)) 
  {
    die('<h1>Nope:</h1>' . mysql_error());
  }
mysql_close($con);
?>

and trying:

$reset = "ALTER TABLE `users` DROP `ID`;
ALTER TABLE `users` AUTO_INCREMENT = 1;
ALTER TABLE `users` ADD `ID` int UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;`";

also yielded no results.

Strangness of it all

Both the $reset commands I tried both execute perfectly in MySQL, but for some reason they fail to act properly from PHP.


Answer

As pointed out the answer, @ variables are preserved per connection, so it is perfectly reasonable to run multiple queries:

///Trigger multiple queries
$nope = '<h1>Nope:</h1>  ';
$res1 = "SET @num := 0;";
$res2 = "UPDATE `users` SET `ID` = @num := (@num+1);";
$res3 = "ALTER TABLE `users` AUTO_INCREMENT =1;";
if (!mysql_query($res1,$con)) die($nope . mysql_error());
if (!mysql_query($res2,$con)) die($nope . mysql_error());
if (!mysql_query($res3,$con)) die($nope . mysql_error());
mysql_close($con);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:44:30+00:00Added an answer on June 7, 2026 at 3:44 am

    mysql_* does not support running multiple queries. You have to run them separately

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Table which has a Primary Key field which is not set
I'm working on a table in a database which has a primary key field
Problem: I am trying to build a recursive tree using a function and data
I'm having a problem using WCF Data Services for an entity that has a
I have a MySQL table with a primary key field that has AUTO_INCREMENT on.
I'd like to save my files using the primary key of the entry. Here
I have two database tables: 'MyTable' which has a standard auto-incrementing primary key integer
I have a problem saving a ModelForm when using a foreign key that doesn't
Django by default makes a primary key field on each model named id ,
This is a pretty simple problem. Inserting data into the table normally works fine,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.