PDO查询,WHERE x喜欢?没有返回任何结果

时间:2019-10-24 20:42:21

标签: sql pdo

我目前正在将网站切换到PDO,但遇到了一些问题,可惜我对此知之甚少。

我有这个查询

$localnewssql = "SELECT n.newsID, n.type, n.title, n.headline, n.tagline, n.body, n.tags, n.date, ni.imageID, i.imageID, i.filename FROM tbl_news_articles AS n 
        LEFT JOIN tbl_news_images AS ni ON ni.newsID = n.newsID 
        LEFT JOIN tbl_images AS i ON i.imageID = ni.imageID
        WHERE n.tags LIKE ? 
        ORDER BY n.date DESC LIMIT 0, 6";
$param = "%sennen%";
$stmt = $conn->prepare($localnewssql);
$stmt->execute($param);
$localnews_data = $stmt->fetchAll();

但是当我foreach循环$localnews_data时,什么也没有返回。有人可以帮我理解吗?

我返回结果的旧MySQL代码是:

$localnewssql = "SELECT n.newsID, n.type, n.title, n.body, n.tags, n.date, ni.imageID, i.imageID, i.filename FROM tbl_news_articles AS n 
        LEFT JOIN tbl_news_images AS ni ON ni.newsID = n.newsID 
        LEFT JOIN tbl_images AS i ON i.imageID = ni.imageID
        WHERE n.tags LIKE '%sennen%' 
        ORDER BY n.date DESC LIMIT 0, 6";
$localnewsresult = mysqli_query($conn,$localnewssql);

不确定PDO版本为何返回空白数组吗?

有什么想法吗? 非常感谢

1 个答案:

答案 0 :(得分:2)

您需要将参数值数组传递给PDOStatement::execute,在此处仅传递字符串。将$param放入数组中即可使用,即

$stmt->execute(array($param));