我尝试在另一个选择查询中绑定一个值数组$ownco
,但它不起作用。我如何才能实现它,所有的值都在第二个查询中被检查/传递?
<?php
$hostname = 'localhost';
$user = 'root';
$password = '';
$username = $_COOKIE['username'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=searchfood", $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT id_post
FROM comments
WHERE username = $username
ORDER BY id DESC"; // oder (longitude between $loo and $lo or latitude between $laa and $la) versuchen
if ($res = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$ownco = $res->fetchAll();
}
} catch (PDOException $e) {
echo $e->getMessage();
}
$userid = $_COOKIE['userid'];
try {
$dbh = new PDO("mysql:host=$hostname;dbname=searchfood", $user, $password);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // <== add this line
$sql = "SELECT id, autorid, autor, date, longitude, latitude, title, text, town, time
FROM posts
WHERE id = $ownco
ORDER BY id DESC"; // oder (longitude between $loo and $lo or latitude between $laa and $la) versuchen
if ($res = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$resultcom = $res->fetchAll();
}
} catch (PDOException $e) {
echo $e->getMessage();
}
?>
你的问题有几个问题。
- 首先,你接受了不回答的答案。违反规则?
- 第二,你的第一个查询将永远不会工作,由于错误的SQL语法和缺乏准备语句。
- 第三,
$ownco
不包含您期望的数据结构。 - 第四,绑定数组与PDO是一个非常简单的问题,在许多答案中已经解释过了,甚至在PDO标签wiki中。
- 第五,根本不需要第二个查询。相反,您必须在第一个查询中使用
JOIN
。
更改第二个查询
$sql = "SELECT id, autorid, autor, date, longitude, latitude, title, text, town, time
FROM posts
WHERE id in (" . implode(",",$ownco) . ")
ORDER BY id DESC";