截断表mysql php提供错误

时间:2016-07-19 07:43:35

标签: php mysqli truncate

我在输入更多数据之前尝试截断表时收到此错误“解析错误:语法错误,意外'表'(T_STRING)”。

    <?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);
     ?>

1 个答案:

答案 0 :(得分:2)

您不能在PHP中以纯文本形式发送MySQL查询。将其作为字符串写入 mysqli_query() 以及当前的打开连接如下图所示:

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

您的整个代码应为:

<?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);
 ?>