选择具有数组值的查询

时间:2015-09-30 20:53:39

标签: php mysql arrays pdo

我将第一个查询中的任何值保存到数组$ownco中。使用第二个查询,我尝试获取表id具有与数组$ownco中相同值的表格帖子的所有行。

两个错误:

  

SQLSTATE [42S22]:未找到列:1054未知列'数组'   'where clause'

     

:第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();
                    }    

1 个答案:

答案 0 :(得分:0)

您必须更改提取样式。目前您正在使用默认&#34; FETCH_BOTH&#34;结果如下:

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

如果您将fetchAll更改为仅提取列,则应该按预期工作。

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

结果:

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