试图调试INNER JOIN难题

时间:2013-05-02 18:48:38

标签: php mysql inner-join

我使用较小的函数来尝试调试更大的函数的内连接。我是一个没有使用连接经验的新手。事实上,我刚刚开始尝试使用内部连接两三天前作为我的第一次连接体验。较小的功能如下:

$data3 = '\'' . implode('\', \'', $posted_email) . '\'';
echo "post email var dump:   ";
var_dump($posted_email);
$exam_recordation = mysql_query("SELECT Student_email FROM exam WHERE partner_id IN (SELECT partner_id FROM exam WHERE Student_email = $data3) AND Student_email <> $data3");
echo "<br>recordation query:   ";
echo "SELECT Student_email FROM exam WHERE partner_id IN (SELECT partner_id FROM exam WHERE Student_email = $data3) AND Student_email <> $data3";
echo "<br>recordation var dump:   ";
var_dump($exam_recordation);
echo "<table border=='1'>
<tr>
<th>Name</th>
</tr>";
while ($fetch_record = mysql_fetch_array($exam_recordation))
{
    echo "<tr>";
    echo "<td>" . $fetch_record['Student_email'] . "</td>";
    echo "</tr>";
}
echo "</table>";
//inner join should use the email of the partner out of $exam query to find their name and print it
$name_query = mysql_query("SELECT DISTINCT student.name
              FROM student
              INNER JOIN exam ON student.email = exam.Student_email
              WHERE student.email = $exam_recordation[Student_email]");
echo "<br><br>INNER JOIN query:   ";
echo "SELECT DISTINCT student.name
              FROM student
              INNER JOIN exam ON student.email = exam.Student_email
              WHERE student.email = $exam_recordation[Student_email]";

echo "<br>query result dump:   ";
var_dump($name_query);

所有结果如下:

post email var dump:
array
  'Student_email' => string 'nuts@prof.com' (length=13)

recordation query: SELECT Student_email FROM exam WHERE partner_id IN (SELECT partner_id FROM exam WHERE Student_email = 'nuts@prof.com') AND Student_email <> 'nuts@prof.com'
recordation var dump:
resource(6, mysql result)
Name
foiling@gorilla.com


INNER JOIN query: SELECT DISTINCT student.name FROM student INNER JOIN exam ON student.email = exam.Student_email WHERE student.email = 
query result dump:
boolean false

你问的问题在哪里?在INNER JOIN中,我无法弄清楚为什么它不会将$ exam_recordation查询中的电子邮件带入其中,即使它已在表中被选中。在更大的函数中,我会让INNER JOIN在while循环中使用记录查询来生成我希望的所需效果。我一直在努力解决这个问题,因为我试图修复/寻求帮助来修复整个主要功能都失败了,我想我会尝试这个问题的INNER JOIN,因为它似乎是头撞砖墙的罪魁祸首。

1 个答案:

答案 0 :(得分:0)

$exam_recordation = mysql_query("SELECT Student_email FROM exam WHERE partner_id IN (SELECT partner_id FROM exam WHERE Student_email = $data3) AND Student_email <> $data3");
echo "<br>recordation query:   ";
echo "SELECT Student_email FROM exam WHERE partner_id IN (SELECT partner_id FROM exam WHERE Student_email = $data3) AND Student_email <> $data3";
echo "<br>recordation var dump:   ";
var_dump($exam_recordation);
echo "<table border=='1'>
<tr>
<th>Name</th>
</tr>";
while ($fetch_record = mysql_fetch_array($exam_recordation))
{
    echo "<tr>";
    echo "<td>" . $fetch_record['Student_email'] . "</td>";
    echo "</tr>";
}

$name_query = mysql_query("SELECT DISTINCT student.name
              FROM student
              INNER JOIN exam ON student.email = exam.Student_email
              WHERE student.email = $exam_recordation[Student_email]");

请看这里,您使用变量$ fetch_record获取查询,但稍后您使用的是$ exam_recordation,它只是mysql查询资源,但不是获取的数据。