我目前正在将网站切换到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版本为何返回空白数组吗?
有什么想法吗? 非常感谢
答案 0 :(得分:2)
您需要将参数值数组传递给PDOStatement::execute
,在此处仅传递字符串。将$param
放入数组中即可使用,即
$stmt->execute(array($param));