Help us understand the problem. What is going on with this article?

ページングのSQL実行時にハマった事 

商品テーブルから名前の情報を取ってくる際のSQL

SELECT name FROM product LIMIT :span OFFSET :p_num

いつものように

$data = array(':span' => $span,'p_num' => $p_num);

と書いてexecuteすると

ERROR!SQLSTATE[42000]: Syntax error or access violation: 1064 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

というエラーが出てきて怒られる。

要は':span' => $span のようにすると、入れられる値は文字の数字になってしまうので、
数値を入れるべきSQL文でエラーが起きた。

解消法

$stmt->bindValue(':span', $span, PDO::PARAM_INT);
$stmt->bindValue('p_num', $p_id, PDO::PARAM_INT);

とすることで、数値として値をSQLに入れられる。
似たものでbindParamというものがあるが、違いは後で調べて追記しておく。

Kyo-Hi
稼げるWEBエンジニアを目指してます | 年収1000万を5年以内に達成が目標
https://twitter.com/yato_san_dayo
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした