2015-05-29 83 views
0

我想在while循环中运行while循环,但只显示单行。为什么while循环显示单行?

<?php  
    if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "SELECT u.id, u.firstname, u.lastname, u.email, c.fullname, ro.shortname, c.id as courseid 
from mdl_context cx 
Left join mdl_course c ON cx.instanceid = c.id 
Left join mdl_role_assignments r on r.contextid = cx.id 
Left join mdl_user u on u.id = r.userid 
Left join mdl_role ro on ro.id = r.roleid 
WHERE r.roleid IN (11) 
AND cx.contextlevel =50 GROUP BY u.id ORDER BY u.id ASC"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
    // output data of each row 
    echo "<table>"."<tr>"."<td width='100px;'><h4>Name</h4></td>"."<td width='50px;'>"."</td>"."<td width='230px;'><h4>Email</h4></td>"."<td width='100px;'><h4>Role</h4></td>"."<td width='100px;'>"."</td>"."<td><h4>Course</h4></td>"."</table>"."<br>"; 
    while($tmg = $result->fetch_assoc()) { 
    echo "<table>"."<tr>"."<td width='100px;'><a href='http://localhost/user/profile.php?id=".$tmg["id"]."'>".$tmg["firstname"]." ".$tmg["lastname"]."</a></td>"."<td width='50px;'>"."</td>"."<td width='230px;'>".$tmg["email"]."</td>"."<td width='100px;'>".$tmg["shortname"]."</td>"; 
    while($row = $result->fetch_assoc()) { 
    echo "<td width='100px;'>"."</td>"."<td>".$row["fullname"]."</td>"."</table>"."<br>"; 
    } 
    } 
} else { 
    echo "0 results"; 
} 
$conn->close(); 
?> 

任何指导或帮助将不胜感激。

+2

怎么样执行另一个查询'$ sqlt' –

+0

没有定义这个“$ sqlt”或者它只是一个错字:P – Sherlock

+0

怎么样的另一个查询? – MOZ

回答

1

一个错字需要执行另一查询$sqlt作为

$sqlt = "SELECT u.firstname, u.lastname, c.id, c.fullname FROM mdl_course AS c JOIN mdl_context AS ctx ON c.id = ctx.instanceid JOIN mdl_role_assignments AS ra ON ra.contextid = ctx.id JOIN mdl_user AS u ON u.id = ra.userid WHERE u.id IN ($newarray)"; 
$result2 = $conn->query($sqlt);//forgot to execute 
while($row = $result2->fetch_assoc()) { 
+0

我已经纠正了代码,现在外循环执行一次!为什么? – MOZ

+0

为什么你两次提取相同的数组 –

+0

因为在1st我想要唯一的记录,第二我想显示所有与第一个数组ID相关的课程。 – MOZ