PDO SELECT LIKE没有找到任何东西

时间:2015-04-13 08:23:15

标签: php pdo

嘿伙计们,所以我试图在标题中找到任何类似Angel的东西,但由于某种原因,它没有返回任何例子,我得到了一个电影标题

黑衣女人2:死亡天使(2014)

但它一直说没有发现任何帮助会是非常棒的谢谢!

<?php
require_once("../connection/config.php");
global $instance;

$search="Angel";
$query = $instance->prepare("select * from movies where title LIKE '%$search%'  LIMIT 0 , 10");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();

         if (!$query->rowCount() == 0) {
                echo "Search found :<br/>";
                echo "<table>";
                echo "<tr>Movies</tr>";
            while ($results = $query->fetch()) {
                echo "<tr>";
                echo $results['title'];
                echo "</tr>";
            }
                echo "</table>";
        } else {
            echo 'Nothing found';
        }
?>

3 个答案:

答案 0 :(得分:1)

这应该是 -

$query = $instance->prepare("select * from movies where title LIKE :val  LIMIT 0 , 10");
$query->bindValue(':val', "%$search%", PDO::PARAM_STR);

答案 1 :(得分:0)

尝试

$query = $instance->prepare("select * from movies where title LIKE :search LIMIT 0, 10"); $query->bindParam(':search, '%'.$search.'%'); $query->execute();

答案 2 :(得分:0)

我认为您不能在绑定时将变量插入引号中,否则它将转换为字符串。我相信你需要事先添加通配符。

$search="%Angel%";
$query = $instance->prepare("select * from movies where title LIKE ? LIMIT 0 , 10");
$query->bindValue(1, $search, PDO::PARAM_STR);