2015-09-30 55 views
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(); 
        }  
+0

你可以分享'的var_dump(破滅( 「」,$ ownco))'結果? –

+2

不需要這2個查詢,這可以在一個連接中完成 –

+1

您可以做一個子查詢:'SELECT id,.... FROM posts WHERE id in(SELECT id_post FROM comments WHERE ....)ORDER BY ID DESC' – Steve

回答

0

您必須更改提取樣式。目前使用的是默認的「FETCH_BOTH」導致類似:如果更改使用fetchall只取預期它應該工作列

Array 
(
    [0] => Array 
     (
      [name] => pear 
      [0] => pear 

$ownco = $own->fetchAll(PDO::FETCH_COLUMN, 0); 

結果:

Array 
(
    [0] = 1, 
    [1] = 3,