MySQL SELECT查询只返回一行
问题描述:
需要以下帮助。我想调用所有有关的行,但我只能得到一个。 我已经尝试过所有可能的方法来输出这个,我得到的只是来自不同会话的一个结果。 getID
函数调用活动会话用户标识,但结果对于所有会话都是相同的。MySQL SELECT查询只返回一行
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)
我有数据库
答
在要覆盖或重新创建阵列$child
每次迭代3个客户条目/行,因此在最后你只在数组$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']
)
);
+0
谢谢@Akshay Hegde。第一个查询工作 –
'在paren上t_id =?' –
您正在每次迭代中重新创建数组。您需要在循环外定义数组并将其添加到循环中。 – shmosel
@Sougata Bose。那就是我加入表格 –