我试图用PDO和mysqli准备的语句解决它,但它仍然返回false。因此,仅sql语句可能出了点问题,但我不知道到底是什么。
$query = $db->prepare('SELECT * FROM bricks WHERE "text" LIKE CONCAT("%", :phrase, "%") AND tags LIKE CONCAT("%", :tag, "%") ORDER BY hearts DESC LIMIT {$start},{$pagesOnSite}');
$query->bindValue(':phrase', $phrase, PDO::PARAM_STR);
$query->bindValue(':tag', $tag, PDO::PARAM_STR);
$query->execute();
我也这样尝试过:
$sql='SELECT * FROM bricks WHERE "text" LIKE CONCAT("%", ?, "%") AND tags LIKE CONCAT("%", ?, "%") ORDER BY hearts DESC LIMIT {$start},{$pagesOnSite}';
$stmt = $db->prepare($sql);
$stmt->bind_param("ss", $phrase, $tag);
$stmt->execute();
答案 0 :(得分:2)
如果要绑定用通配符括起来的某些文本文字,则应在PHP代码中构建整个字符串值。然后,将其绑定到您的语句。
$sql = "SELECT * FROM bricks ";
$sql .= "WHERE text LIKE :phrase AND tags LIKE :tag ";
$sql .= "ORDER BY hearts DESC LIMIT {$start}, {$pagesOnSite}";
$query = $db->prepare($sql);
$query->bindValue(':phrase', '%'.$phrase.'%', PDO::PARAM_STR);
$query->bindValue(':tag', '%'.$tag.'%', PDO::PARAM_STR);
$query->execute();