首页 文章

选择具有数组值的查询

提问于
浏览
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();
                    }

1 回答

  • 0

    您必须更改提取样式 . 目前您正在使用默认的“FETCH_BOTH”,其结果如下:

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

    如果将fetchAll更改为仅获取列,则应按预期工作 .

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

    结果:

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

    等等

相关问题