MySQL SELECT查询只返回一行

时间:2017-01-01 18:30:46

标签: php mysql

需要以下帮助。我想打电话给所有有关行但我只得到一个。 我已经尝试了所有可能的方法来输出这个,我得到的是来自不同会话的一个结果。 getID函数调用活动会话用户ID,但所有会话的结果都相同。

    public function getChildId()
{
    $child_id = $this->db->query("SELECT  firstname, lastname
        FROM " . $this->db->table("customers") . "
        RIGHT JOIN " . $this->db->table("genealogy") . " on parent_id
                WHERE parent_id = '". $this->customer->getID(). "'"
    );
   foreach ($child_id as $child => $child_id->num_rows ) {
           $child = array(
              $this->name = $child_id->row['firstname'],
               $this->surname = $child_id->row['lastname']
           );
   }
    return $child;
}

}

var_dump ($child)我得到:array (size=2) 0 => string 'John' (length=8) 1 => string 'Doe' (length=9) 我在数据库上有3个客户条目/行

1 个答案:

答案 0 :(得分:1)

在每次迭代中,您将覆盖或重新创建数组$child,因此最后您只在数组$child中只有一个元素(迭代的最后一行)。

更改

$child = array(
              $this->name = $child_id->row['firstname'],
               $this->surname = $child_id->row['lastname']
           );

(在每次迭代中将一个或多个元素推到数组的末尾)

$child[] = array(
              $this->name = $child_id->row['firstname'],
               $this->surname = $child_id->row['lastname']
           );

您也可以使用下面的array_push

array_push(
           $child,
           array(
                  $this->name = $child_id->row['firstname'],
                   $this->surname = $child_id->row['lastname']
               )
           );
相关问题