Truncate table mysql php providing error

php mysqli truncate

4056 просмотра

1 ответ

5 Репутация автора

I am receiving this error "Parse error: syntax error, unexpected 'TABLE' (T_STRING)" when trying to truncate a table before more data is entered.

    <?php
      $servername = "localhost";
      $username = "root";
      $password = "";
      $dbname = "members";

      // Create connection
      $conn = mysqli_connect($servername, $username, $password, $dbname);
      // Check connection
      if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
      }

      TRUNCATE TABLE users;

      {
        header ("Location: order.php");
        exit;
      }

       mysqli_close($conn);
     ?>
Автор: Sam Hoinville Источник Размещён: 19.07.2016 07:43

Ответы (1)


2 плюса

8129 Репутация автора

You can't just put MySQL queries in plain text in PHP. Write it as a string inside mysqli_query() along with your current open connection as shown below:

mysqli_query($conn, "TRUNCATE TABLE `users`");

Your entire code should be:

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "members";

    // Create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // Check connection
    if (!$conn) {
        die("Connection failed: " . mysqli_connect_error());
    }
    else {
        mysqli_query($conn, "TRUNCATE TABLE `users`");
        header ("Location: order.php");
        exit;
    }

   mysqli_close($conn);
 ?>
Автор: Angel Politis Размещён: 19.07.2016 07:46
Вопросы из категории :
32x32