我目前正在运行一个在 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循环中缺少的东西,可能不是。我真的已经没有想法了。