0
我用第一個查詢將任意值保存到數組$ownco
。通過第二個查詢,我嘗試獲取id
與陣列$ownco
中的值相同的所有表格帖子行。用數組值選擇查詢
兩個錯誤:
SQLSTATE [42S22]:柱未找到:1054未知列在 '陣列' 'where子句'
:Array對字符串的轉換在管線34
$hostname='localhost';
$user='root';
$password='';
$useron = $_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 = '$useron'
ORDER BY id DESC"; // oder (longitude between $loo and $lo or latitude between $laa and $la) versuchen
if ($own = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$ownco = $own->fetchAll();
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
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 in (" . implode(",",$ownco) . ") // line 34
ORDER BY id DESC"; // oder (longitude between $loo and $lo or latitude between $laa and $la) versuchen
if ($resco = $dbh->query($sql)) {// need to add this line in your code
// then after fetchColumn
$resultcom = $resco->fetchAll();
}
}
catch(PDOException $e)
{
echo $e->getMessage();
}
你可以分享'的var_dump(破滅( 「」,$ ownco))'結果? –
不需要這2個查詢,這可以在一個連接中完成 –
您可以做一個子查詢:'SELECT id,.... FROM posts WHERE id in(SELECT id_post FROM comments WHERE ....)ORDER BY ID DESC' – Steve