mysql数据库sql语法错误

时间:2015-12-19 20:14:41

标签: php mysql ajax database

所以我有这个奇怪的问题:下面你可以看到我的PHP代码:

if($type == "kilometers") {
    $sql = "SELECT * FROM transports WHERE user='".$user."' AND (theDate BETWEEN '".$fromDateFormatted."' AND '".$toDateFormatted."')";
    $status = mysqli_query($conn, $sql);

    if(!$status) {
        $result = $conn->error;
        die($result);
    }

    $num_rows = mysqli_num_rows($status);
    for($i = 0; $i < $num_rows; $i++) {
        $data = mysqli_fetch_assoc($status);

        $results[$i] = $data;

        $date = strtotime($data["theDate"]);
        $date = date("d/m/Y", $date);

        $results[$i]["formattedDate"] = $date;
    }
}else if($type == "vacation") {
    $sql = "SELECT * FROM leave";
    $status = mysqli_query($conn, $sql);

    if(!$status) {
        $result = $conn->error;
        die($result);
    }

    $num_rows = mysqli_num_rows($status);
    for($i = 0; $i < $num_rows; $i++) {
        $data = mysqli_fetch_assoc($status);

        $results[$i] = $data;

        $fromDate = strtotime($data["fromDate"]);
        $fromDate = date("d/m/Y", $fromDate);
        $toDate = strtotime($data["toDate"]);
        $toDate = date("d/m/Y", $toDate);

        $results[$i]["formattedDate"] = $fromDate;
        $results[$i]["formattedToDate"] = $toDate;
    }
}

此代码由ajax调用启动,该调用会发布&#39;类型&#39;变量。随着不同类型提供不同的信息。第一个公里&#39;查询返回没有错误,但第二个假期&#39;查询给我以下错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'leave' at line 1

但正如您在下图中所看到的,两个表都已创建。有什么想法吗?

The Error

1 个答案:

答案 0 :(得分:1)

Leave是mysql中的保留关键字。

SELECT * FROM `leave`

https://dev.mysql.com/doc/refman/5.7/en/leave.html