0

How do i display mysql query result as an array

mydeptid is an array

$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";
3
  • What's your issue? Commented May 26, 2017 at 5:16
  • use where IN clause Commented May 26, 2017 at 5:16
  • it display only one result Commented May 26, 2017 at 5:16

2 Answers 2

2

As per query, I understand, you have an array of department IDs. Hence, you will have to convert them into string before using them in a query. Check and try the below code:

$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";
Sign up to request clarification or add additional context in comments.

Comments

0

Use a single query with a JOIN

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.