我目前正在运行一个在while循环中使用内部联接的函数,这个函数无意中返回任何内容。
$data3 = '\'' . implode('\', \'', $posted_email) . '\'';
$exam_recordation = mysql_query("SELECT * FROM exam WHERE partner_id IN (SELECT partner_id FROM exam WHERE Student_email = $data3) AND Student_email <> $data3");
//$name_query = mysql_query("SELECT student.email, student.name, Student_email FROM student INNER JOIN exam ON student.email = exam.Student_email WHERE student.email = $data3")
echo "<table border=='1'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Exam Date</th>
<th>Level</th>
<th>Mark</th>
<th>Style</th>
</tr>";
while($row = mysql_fetch_array($exam_recordation))
{
echo "<tr>";
$name_query = mysql_query("SELECT DISTINCT student.name
FROM student
INNER JOIN exam ON student.email = exam.Student_email
WHERE student.email <>{$data3}");
//die();
/*while($row1 = mysql_fetch_array($name_query))
{
echo "<td>" . $row1['name'] . "</td>"; //This while loop was just me overcomplicating it, soon worked out I didnt need it
}*/
echo "<td>" . $name_query['name'] . "</td>";
echo "<td>" . $row['Student_email'] . "</td>";
echo "<td>" . $row['examDate'] . "</td>";
echo "<td>" . $row['level'] . "</td>";
echo "<td>" . $row['mark'] . "</td>";
echo "<td>" . $row['style'] . "</td>";
echo "</tr>";
}
echo "</table>";
从我自己尝试调试它,我已经意识到它返回的不是我希望的值(学生表中的名称),而是当我在一个独立的PHP函数中运行它时自己的NULL值,即不在里面一个while循环如下所示。
$data3 = '\'' . implode('\', \'', $posted_email) . '\'';
var_dump($posted_email);
var_export($data3);
$name_query = mysql_query("SELECT DISTINCT student.name
FROM student
INNER JOIN exam ON student.email = exam.Student_email
WHERE student.email <> $data3");
var_export($name_query);
echo "SELECT DISTINCT student.name
FROM student
INNER JOIN exam ON student.email = exam.Student_email
WHERE student.email <> $data3";
但是,当我从SQL中的最后一段代码运行echo'd INNER JOIN时,它工作正常并返回2行名称。
SELECT DISTINCT student.name FROM student INNER JOIN exam ON student.email = exam.Student_email WHERE student.email <> 'eating@gnomes.com'
我正在努力的理论是:
$name_query = mysql_query("SELECT DISTINCT student.name
FROM student
INNER JOIN exam ON student.email = exam.Student_email
WHERE student.email <>{$data3}");
while($row = mysql_fetch_array($exam_recordation) && mysql_fetch_array($name_query))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['Student_email'] . "</td>";
echo "<td>" . $row['examDate'] . "</td>";
echo "<td>" . $row['level'] . "</td>";
echo "<td>" . $row['mark'] . "</td>";
echo "<td>" . $row['style'] . "</td>";
echo "</tr>";
}
echo "</table>";
这对我的表没有任何回报,但我想要做的是在一次获取中使用两个查询,以便将它们的结果都放入$ row并能够提取相关数据。这实际上听起来相当遥远,所以如果我认为这是缺乏经验的错误。
正如我希望你能看到的那样,我试图找出问题所在,但失败了。通常只是检查发送的查询是否正确已经解决了这种过去没有为我返回的问题,但对于这个问题似乎并非如此。可能是我在数组或while循环中缺少的东西,可能不是。我真的没想到了。
答案 0 :(得分:0)
你确定你有这个学生的考试成绩吗?
INNER JOIN仅在两个连接表中都存在数据时返回结果。
尝试使用LEFT JOIN查看是否返回。
还可以在phpMyAdmin或MySQL控制台等测试您的查询,以确定是否是您处理结果时的查询。
答案 1 :(得分:0)
您评论了fetch方法,它无法解决问题。 这就是你应该采取名称的方式:
$rowStudent = mysql_fetch_array($name_query);
echo "<td>" . $rowStudent['name'] . "</td>";
mysql_query只发送和执行查询,以获得需要获取的结果。
另外,您可以检查是否有if($rowStudent)
或if(!$rowStudent)
的结果来处理没有结果。
答案 2 :(得分:0)
你应该试试这个,它只有一个连接这两个表的查询。
$data3 = '\'' . implode('\', \'', $posted_email) . '\'';
$exam_recordation = mysql_query("SELECT student.name, exam.Student_email,
exam.examDate, exam.level, exam.mark, exam.style
FROM student INNER JOIN exam ON student.email = exam.Student_email
WHERE student.email <> $data3");
echo "<table border=='1'>
<tr>
<th>Name</th>
<th>Email</th>
<th>Exam Date</th>
<th>Level</th>
<th>Mark</th>
<th>Style</th>
</tr>";
while($row = mysql_fetch_array($exam_recordation))
{
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['Student_email'] . "</td>";
echo "<td>" . $row['examDate'] . "</td>";
echo "<td>" . $row['level'] . "</td>";
echo "<td>" . $row['mark'] . "</td>";
echo "<td>" . $row['style'] . "</td>";
echo "</tr>";
}
echo "</table>";