0

I want to reset a database table id by the SQL command in PHP when I click a button but unfortunately, the SQL command work on the PHPmyadmin SQL option, not working in PHP.

I am getting the error:

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'UPDATE wordmeanings_table SET id = @num := (@num+1); ALTER TABLE wordmeanings...' at line 1 in E:\xampp\htdocs\php-ajax\index.php:98 Stack trace: #0 E:\xampp\htdocs\php-ajax\index.php(98): mysqli_query(Object(mysqli), 'SET @num := 0;...') #1 {main} thrown in E:\xampp\htdocs\php-ajax\index.php on line 98

Check out the PHP codes, I don't know what's wrong with the codes:

<?php
if(isset($_POST['submit']))
{
  $sql = "SET  @num := 0; UPDATE wordmeanings_table SET id = @num := (@num+1); ALTER TABLE wordmeanings_table AUTO_INCREMENT =1";

  mysqli_query($conn, $sql);
    
};
?>
  • Unless you're using [mysqli_multi_query](https://www.php.net/manual/en/mysqli.multi-query.php), you cannot have multiple statements in one query. You'll need to separate them out – aynber Dec 30 '22 at 13:46
  • I'm assuming you are using MySQL, your sql-server tag (I'm assuming) is a mistake. – Nigel Ren Dec 30 '22 at 13:48
  • @aynber I have tried this way as well but not working https://prnt.sc/DfcfmohcViCV – Najmul Hasan Dec 30 '22 at 13:55
  • Because you're overwriting `$sql` before you try to run the query. The query you show here, not in your screenshot, should work with mysqli_multi_query. The queries in your screenshot need to have mysqli_query run after each statement. – aynber Dec 30 '22 at 13:59

0 Answers0