LoginSignup
0
0

More than 5 years have passed since last update.

SQLエラー:SQLSTATE[42000]〜check the manual that corresponds to your MySQL 〜

Posted at

curlコマンドで自分で作ったAPIのデータを取得しようとしたところ次のようなエラーがでて落ち込む。

"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 'INNER JOIN price AS p ON h.id = p.hotel_id
WHERE
  p.target_date = '20190205'
  ' at line 9"

ただ読んでみると、意外と簡単に解決できそうで要は
「オメ--SQLの書き方間違ってんだよ!」

ということで調べてみると

$sql = "
SELECT
  p.target_date,
  h.name,
  h.area,
  p.min_price,
  h.link
FROM
  hotel AS h
  INNER JOIN price AS p ON h.id = p.hotel_id
WHERE
  p.target_date = ?
  AND h.place = ?
";
        if (!empty($condition["order"]) && ($condition["order"] == "high")) {
               $sql .= " ORDER BY p.max_price DESC ";
           } elseif (!empty($condition["order"]) && ($condition["order"] == "low")) {
               $sql .= " ORDER BY p.min_price ASC ";
           }
           if (!empty($condition["list"]) && ($condition["list"] <= 20) && (is_numeric($condition["list"]))) {
               $sql .= " LIMIT " . $condition["list"];
           } else {
               $sql .= " LIMIT 20 ";
           }

このSQL文の

$sql .= " ORDER BY p.max_price DESC ";

ORDERの前に空白が入っていないのが問題でした(上は修正後)

皆さんも空白忘れやカンマ忘れに気をつけましょう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0