我的链接表中有一个链接和标题列表,我尝试使用 IN 子句返回它们,但查询永远不会返回超过一行的内容,并且始终返回集合的最小行。
SELECT title, url
FROM opsx_links
WHERE linkid IN('61','60','10','24','15','20','30','47')
这应该返回所有 8 个链接,因为它们都存在,但它只返回第 10 项的信息。如果我从列表中删除第 10 项,它只会返回第 15 项,依此类推。
我会失去它还是什么?
我用 Google 搜索了一下,但找不到任何遇到此问题的人。
谢谢。
-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;
关于php - IN 子句仅返回 1 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9986092/