2015-09-30 56 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,