Reputation: 43
I've been working and testing with queries, and I came across this issue which I don't understand why it's happening.
I have this code:
<?php
$query = "
SELECT sc.course_code
, c.course_name
FROM student_course sc
, courses c
WHERE sc.course_code= 'MC 101'
AND sc.id_num='A0001'
";
$result = mysqli_query($conn, $query);
if(mysqli_num_rows($result) > 0)
{
while($row = mysqli_fetch_array($result))
{
?>
<p> <h4 style="w3-wide"> <?php echo $row["course_name"];?> </h3>
<h4 style="w3-wide"><?php echo $row["course_code"];?> </h3>
<button type="button" class="w3-button w3-theme-d2 w3-margin-bottom" style = "float:right"><i></i><a href="schedule mc101.php"> View Course</a></button></p>
<br>
And based on what I know, it should call a single data which satisfies the conditions student_course.course_code = 'MC 101'
and student_course.id_num='A0001'
which should only be
Statistics in Computing
MC 101
However, this is what I'm getting:
The course names are correct, but all of them have the same code. Does anyone know why this is happening? Much appreciated!
Upvotes: 2
Views: 38
Reputation: 14110
You missed a join condition,
it must me something like ON sc.course_id = c.id
SELECT sc.course_code, c.course_name
FROM student_course sc
JOIN courses c ON sc.course_id = c.id
WHERE sc.course_code= 'MC 101'
AND sc.id_num='A0001'
Upvotes: 1