将mysql查询数据显示为数组

时间:2017-05-26 05:13:30

标签: php mysql arrays

如何将mysql查询结果显示为数组

mydeptid是一个数组

$sql = "SELECT * FROM ".$DB->prefix("settings")." WHERE uid='$myuid'";
$result = $DB->query($sql);

while($row = $DB->fetchArray($result))
{
$uid=$row['uid'];
$mydeptid[]=$row['deptid'];


}

 $sql="SELECT * FROM DEPARTMENTS WHERE DEPTID=$mydeptid ORDER BY DEPTNAME ASC";

2 个答案:

答案 0 :(得分:2)

根据查询,我了解到,您有一系列部门ID。因此,在查询中使用它们之前,必须将它们转换为字符串。检查并尝试以下代码:

$sql = "SELECT * FROM ".$DB->prefix("settings")." WHERE uid='$myuid'";
$result = $DB->query($sql);

while($row = $DB->fetchArray($result))
{
$uid=$row['uid'];
$mydeptid[]=$row['deptid'];
}

$allDeptIds = implode(",", $mydeptid);

$sql="SELECT * FROM DEPARTMENTS WHERE DEPTID in ($allDeptIds) ORDER BY DEPTNAME ASC";

答案 1 :(得分:0)

使用带JOIN的单个查询

$sql = "SELECT D.* FROM ".$DB->prefix("settings")." T
JOIN DEPARTMENTS D  ON  D.DEPTID=T.deptid WHERE uid='$myuid'";