Deleting all rows from table, and resetting auto incrementation

Keyfer Mathewson

code is below. Seems like when I run this file, it doesn't do anything.

header('Content-Type: application/json');

$DB = new mysqli("localhost", "root", "root", "friendslist");
if($DB->connect_errno) {
    die("Connect failed: ". $DB->connect_error);
}

$DB->query("DELETE * FROM users");
$DB->query("ALTER TABLE users AUTO_INCREMENT = 1");

$DB->close();

    echo json_encode(Array('status' => 'ok'));
Barmar

Your query has incorrect syntax. If you read the documentation, you'll see that the valid syntaxes of DELETE are either:

DELETE FROM users

or:

DELETE users.* FROM users

where .* is optional in the second version. But DELETE * is not valid.

You could also just use TRUNCATE tablename to delete everything from the table and reset the auto increment ID in one step.

You should also check whether queries succeed:

$DB->query("DELETE * FROM users") or die ($DB->error);

would have told you that there was a syntax error. Then I'm sure you would have checked the documentation to see what the correct syntax is, and fixed it right away, rather than going the slow route of posting a question to SO.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Deleting all rows from Cassandra cql table

From Dev

Issue with Deleting rows from my second Table in my page

From Dev

Deleting multiple rows from a table

From Dev

How does deleting rows from a table affect its indexes?

From Dev

Trigger to delete rows from related tables before deleting rows from actual table

From Dev

Resetting zebra style after removing rows from table

From Dev

Deleting all table records from core data database using relationship

From Dev

Deleting rows from 3 tables in MySQL table

From Dev

Deleting all rows from database except first two rows in oracle

From Dev

Resetting Primary key without deleting truncating table

From Dev

Finding duplicate records from table and deleting all but one with latest date

From Dev

PostgreSQL: deleting rows referenced from another table

From Dev

Dynamically added table rows not deleting

From Dev

Deleting all but the most recent entry from single SQL table

From Dev

SQLite: *prevent* PRIMARY KEY value from resetting after delete all rows

From Dev

c# deleting duplicate rows from data table

From Dev

Deleting all rows from table that share the same ID

From Dev

Efficiently deleting rows from one table where not matching another [MySQL]

From Dev

Deleting many rows from a big table MySql 5.5.46

From Dev

Deleting multiple rows from a table

From Dev

Resetting zebra style after removing rows from table

From Dev

Issue deleting rows from displayed table

From Dev

Finding duplicate records from table and deleting all but one with latest date

From Dev

Identify the action that is deleting all rows in a table

From Dev

All records being deleted when deleting records from a temp table

From Dev

MySQL: deleting rows based on a condition with data from another table and NO JOIN

From Dev

Resetting visuals for checked rows in a table via button

From Dev

Deleting rows in HTML table

From Dev

SQL Stored Procedure Deleting all rows in table

Related Related

  1. 1

    Deleting all rows from Cassandra cql table

  2. 2

    Issue with Deleting rows from my second Table in my page

  3. 3

    Deleting multiple rows from a table

  4. 4

    How does deleting rows from a table affect its indexes?

  5. 5

    Trigger to delete rows from related tables before deleting rows from actual table

  6. 6

    Resetting zebra style after removing rows from table

  7. 7

    Deleting all table records from core data database using relationship

  8. 8

    Deleting rows from 3 tables in MySQL table

  9. 9

    Deleting all rows from database except first two rows in oracle

  10. 10

    Resetting Primary key without deleting truncating table

  11. 11

    Finding duplicate records from table and deleting all but one with latest date

  12. 12

    PostgreSQL: deleting rows referenced from another table

  13. 13

    Dynamically added table rows not deleting

  14. 14

    Deleting all but the most recent entry from single SQL table

  15. 15

    SQLite: *prevent* PRIMARY KEY value from resetting after delete all rows

  16. 16

    c# deleting duplicate rows from data table

  17. 17

    Deleting all rows from table that share the same ID

  18. 18

    Efficiently deleting rows from one table where not matching another [MySQL]

  19. 19

    Deleting many rows from a big table MySql 5.5.46

  20. 20

    Deleting multiple rows from a table

  21. 21

    Resetting zebra style after removing rows from table

  22. 22

    Issue deleting rows from displayed table

  23. 23

    Finding duplicate records from table and deleting all but one with latest date

  24. 24

    Identify the action that is deleting all rows in a table

  25. 25

    All records being deleted when deleting records from a temp table

  26. 26

    MySQL: deleting rows based on a condition with data from another table and NO JOIN

  27. 27

    Resetting visuals for checked rows in a table via button

  28. 28

    Deleting rows in HTML table

  29. 29

    SQL Stored Procedure Deleting all rows in table

HotTag

Archive