-1

So I have this weird problem: below you can see my php code:

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;
    }
}

This code is initiated by an ajax call which posts the 'type' variable. With different types comes different information. The first 'kilometers' query returns no errors, but the second 'vacation' query gives me the following error:

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

But as you can see in the following image, both tables are created. Any ideas?

The Error

zeno dhaene
  • 245
  • 3
  • 14
  • 1
    `leave` is a MySQL reserved word - https://dev.mysql.com/doc/refman/5.5/en/keywords.html. So you need to use backticks -> `SELECT * FROM \`leave\`` – Sean Dec 19 '15 at 20:17

1 Answers1

1

Leave is a reserved keyword in mysql.

SELECT * FROM `leave`

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

prem kumar
  • 5,641
  • 3
  • 24
  • 36