2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

LIKEをbindしようとしたら二度怒られた。

Posted at

LIKE検索をPHPからしたい。

bindなんたら~でLIKEを書いたら怒られた。

おこられた書き方.php
$sql = "select * from table where hoge like %:value% ";
$stmt  = $pdo->prepare($sql);
$stmt->bindValue(":value", $value, PDO::PARAM_STR);

え、これも違うのか。

文字列扱いだからクォーテーションがいる!とか思った。

こりてない書き方.php
$sql = "select * from table where hoge like '%:value%' ";
$stmt  = $pdo->prepare($sql);
$stmt->bindValue(":value", $value, PDO::PARAM_STR);

%も変数に入れてください🤬

すみませんでした。。。

ごめんね.php
$sql = "select * from table where hoge like :value ";
$stmt  = $pdo->prepare($sql);
$value = '%'. $value .'%'; // => ここで"%"をつける。
$stmt->bindValue(":value", $value, PDO::PARAM_STR);

探すのに手間取ったのでメモ。

参考

2
1
1

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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?