0

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

4

3 回答 3

0

你评论了 fetch 方法,没有它就行不通。这是你应该取名字的方式:

$rowStudent = mysql_fetch_array($name_query);
echo "<td>" . $rowStudent['name'] . "</td>";

mysql_query 仅发送和执行查询,以获取您需要获取的结果。

你也可以检查你是否得到了结果if($rowStudent)if(!$rowStudent)处理没有结果。

于 2013-05-02T01:57:54.490 回答
0

你确定你有这个学生的一些考试成绩吗?

INNER JOIN 只会在两个连接表中都存在数据时返回结果。

尝试使用 LEFT JOIN 来查看是否返回。

还要在 phpMyAdmin 或 MySQL 控制台之类的东西中测试您的查询,以确定是否是您处理结果的错误查询。

于 2013-05-02T01:16:07.883 回答
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>";
于 2013-05-02T02:42:00.127 回答