IN子句只返回1行


IN Clause return only 1 row

我的链接表中有一个链接和标题列表,我试图使用in子句来返回它们,但查询返回的行永远不会超过一行,而且总是集合的最小行。

SELECT title, url 
FROM opsx_links 
WHERE linkid IN('61','60','10','24','15','20','30','47')

这应该返回所有8个链接,因为它们确实都存在,但它只返回项目10的信息。如果我从列表中删除项目10,它将只返回项目15,依此类推

我是输了还是怎么了?

我在谷歌上搜索了一下,找不到有这个问题的人。

谢谢。

-V

好吧,我的坏消息是这里的php代码

public function getLinks ($data) {
   $query  = $this->db->Fetch ("SELECT title, url 
             FROM {$this->prefix}links WHERE linkid IN(" . $data . ")");
   $result = $this->db->FetchObject ($query);
   foreach ($result as $key => $value):
      $result->$key = $this->replace_strings ($value);
   endforeach;
   $this->db->Free ($query);
   return $result;
}

每个球衣都试过这个

$res = array();
while ($result = $this->db->FetchObject ($query)):
    $res['title'] = $result->title;
    $res['url']   = $result->url;
endwhile;

现在只返回第一行,而不是最小行。

到底是什么?

好吧,经过反复试验和各位大师的帮助,以下是答案。

public function getLinks ($data) {
     $query  = $this->db->Fetch ("SELECT title, url 
               FROM {$this->prefix}links WHERE linkid IN(" . $data . ")");  
     $res = array ();
     while ($results = $this->db->FetchArray ($query)):
          $obj = new stdClass;
          $obj->title = $results['title'];
          $obj->url = $this->replace_strings($results['url']);
          $res[] = $obj;
     endwhile;
     $this->db->Free ($query);
     return (object)$res;
}

谢谢你的帮助。

编辑:您只能从结果集中获得一个结果:

$result = $this->db->FetchObject ($query);

应该是这样的:

$my_results = array();
while ($result = $this->db->FetchObject ($query))
{
  // make a new / clone the object, add it to the array and do the processing
}
return $my_results;