我想知道为什么我找不到解决这个奇怪问题的方法。 对不起,如果我看起来不对,但我非常绝望,并尽可能地解决这个问题。
在递归函数中,我收集sql数据并将它们存储在一个数组中。 但是,当我使用print_r输出数组值时,我会得到如下奇怪的值:
A" parentMale" =' 45273'或者a。" parentFemale" =' 44871'要么 一个" parentMale" =' 7625'或者a。" parentFemale" =' 7481' OR数组或数组 OR数组或数组或a。" parentMale" = ...
虽然我的功能如下:
public function getSelfAndAncestorsShort($id) {
$animalids = array();
if ($id != "")
{
$query = "SELECT a.\"parentMale\" AS sire, a.\"parentFemale\" AS dam
FROM animals a
WHERE a.id = ".$id;
$res = pg_query($query);
while ($row = pg_fetch_object($res))
{
if ($row->sire != "")
$animalids[] = "a.\"parentMale\" = '" .$row->sire ."'";
if ($row->dam != "")
$animalids[] = "a.\"parentFemale\" = '" .$row->dam ."'";
$animalids[] = $this->getSelfAndAncestorsShort($row->sire);
$animalids[] = $this->getSelfAndAncestorsShort($row->dam);
$animalids = implode (" OR ", $animalids);
}
}
return $animalids;
}
我希望有人可以帮助我,因为我真的不知道。
答案 0 :(得分:0)
如果$id
为空,则函数返回一个数组。此外,在添加到$animalids
数组之前,您不会检查函数的返回值:
public function getSelfAndAncestorsShort($id) {
$animalids = array();
if ($id != "")
{
$query = "SELECT a.\"parentMale\" AS sire, a.\"parentFemale\" AS dam
FROM animals a
WHERE a.id = ".$id;
$res = pg_query($query);
while ($row = pg_fetch_object($res))
{
if ($row->sire != "")
$animalids[] = "a.\"parentMale\" = '" .$row->sire ."'";
if ($row->dam != "")
$animalids[] = "a.\"parentFemale\" = '" .$row->dam ."'";
$val = $this->getSelfAndAncestorsShort($row->sire);
if ($val) $animalids[] = $val; // Check here
$val = $this->getSelfAndAncestorsShort($row->dam);
if ($val) $animalids[] = $val; // Check here
}
}
if (empty($animalids)) return "" ; // Check here
return implode (" OR ", $animalids); // Only implode here
}