我想知道为什么我没有找到这个奇怪问题的任何解决方案。对不起,如果我看起来不对,但我非常绝望,并尽可能多地解决这个问题。
在递归函数中,我收集 sql 数据并将它们存储在数组中。但是,当我使用 print_r 输出数组值时,我得到这样的奇怪值:
一。parentMale" = '45273' OR a."parentFemale" = '44871' OR a."parentMale" = '7625' OR a."父女性" = '7481' 或 数组 OR 数组 或数组或阵列或 a."父母男性" = ...
虽然我的函数看起来像这样:
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;
}
我希望有人能帮助我,因为我真的不知道。
如果 $id
为 null,则 U 函数返回一个数组。另外,在添加到$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
}