开发者

Extracting member's first name from member table using results from another table

开发者 https://www.devze.com 2023-03-16 11:29 出处:网络
<?php include \'dbFunctions.php\'; $courseid = $_GET[\'Course_id\']; $query = \"SELECT * FROM course WHERE Course_id=\".$courseid.\"\";
<?php
include 'dbFunctions.php';
$courseid = $_GET['Course_id'];

$query = "SELECT * FROM course WHERE Course_id=".$courseid."";
$arrCourse = executeSelectQuery($query);

$qu开发者_如何学Goery2 = "SELECT * FROM course_member WHERE Course_id=".$courseid."";

$result = mysqli_query($link,$query2) or die(mysqli_error($link));
?>

HTML body:

<?php
while ($row = mysqli_fetch_array($result)) {
?>

      <?php echo $row['member_id']
      ?>

      <?php
}
?>

this correctly prints the results of the member id that is involved in the selected course, however, i want to extract the first_name using the result generated that belong to another table called member.

been trying all the join queries but it didn't work.


Sounds like you need to read up on the JOIN operation in SQL. The following query will return all the data you seem to need, from a single query:

SELECT * 
FROM course
INNER JOIN course_member ON course.id = course_member.course_id
INNER JOIN member ON member.id = course_member.member_id


As far as I see

$sql="select last_name from course_member 
join member on course_member.member_id = member.member_id
where course_member.Course_id = ".$courseid;


I'm going to take a guess here at your table defs...

$query2 = "SELECT m.first_name, cm.* FROM course_member cm join member m on m.id = cm.member_id WHERE Course_id=".$courseid."";


If you want only the member name and you are not much interested with the id then you can merge it into a single query

 $query = SELECT fName from member WHERE id = (SELECT member_id FROM course_member WHERE Course_id=$courseid); 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号