Jump to content

Displaying 2 linked tables


Johnnyboy123

Recommended Posts

So I have 3 tables, student, student_course and course. I'm only using student and course atm, probably should look into student_course haha well those are the tables provided to us for the project I'm working on.

 

I'm trying to list all the courses from the course table and then when clicking on a course I want to display all the students who registered for this course( which would probably be the student table)

 

Here is my code for displaying the courses:

<?php
$display_sql ="SELECT cname FROM course ORDER BY cid DESC";
$display_query = mysql_query($display_sql) or die(mysql_error());
$rsDisplay = mysql_fetch_assoc($display_query);
?>


<html>

<head>

</head>


<body>
<p> Click course to display all students registered for that course</p>
<?php do { ?>
<p><a href="test.php?cname=<?php echo $rsDisplay['cid']; ?>">


Course:
<?php echo $rsDisplay['cname']; ?> 

</a>
</p>


<?php } while ($rsDisplay = mysql_fetch_assoc($display_query)) ?>

 

And here is my code for displaying the students from the student table registered for the specific course you click on:

<?php
$q = "SELECT * FROM student WHERE cname = $_GET[cname]";
$confirm_query = mysql_query($q);
$rsconfirm = mysql_fetch_assoc($confirm_query);
?>
<html>
<head>
</head>
<body>
<p> Displaying all student </p>
<p>
First Name
<?php echo $rsconfirm['sname']; ?>
</p>
<p>
Surname:
<?php echo $rsconfirm['fname']; ?>
</p>

 

I'm very new to php. Im using "test.php?cname=" to transfer the cname (course name) info from my course table to the next page where I use $_GET[cname]";. cname isn't a primary key in my table, rather just a row. Can it be done like that? Or should you just use primary keys?

 

I managed to display the courses but when clicking it, it wont display the registered users and gives me an error:

Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given in C:\Program Files\EasyPHP-5.3.3\www\Project\test.php on line 28

 

What does that mean?

 

Line 28 is this part :

$q = "SELECT * FROM student WHERE cname = $_GET[cname]"; //26
$confirm_query = mysql_query($q);                                                       //27
$rsconfirm = mysql_fetch_assoc($confirm_query);

                    //28

 

Any criticism welcome. When coding I'm still trying to figure out what it is I'm doing nevermind getting it to work haha. Thanks in advance

Link to comment
Share on other sites

$q = "SELECT RegisteredUsers FROM student WHERE cname = {$_GET['cname']}";

 

While you can drop a flat variable between double quotes and it will parse the contents- when you are using an array you need to wrap it inside {} to have it recognised.  In addition, you must always put quotes arround the array key that you are pointing at (otherwise PHP sees it as constant, not a variable.)

 

Also, you need to be consistant with your error capture - you use or die () in the first page during query execution, but not the second.

Link to comment
Share on other sites

is that quicker or less draining on resources or just less code?

It's just easier to manage on bigger pages, using the . concatination method can get quite confusing when there is a lot going on, and makes it easier to make mistakes. 

 

If it were to affect speed or resource it would be so negligable that you would be hard pressed to notice.

Link to comment
Share on other sites

$q = "SELECT RegisteredUsers FROM student WHERE cname = {$_GET['cname']}";

 

While you can drop a flat variable between double quotes and it will parse the contents- when you are using an array you need to wrap it inside {} to have it recognised.  In addition, you must always put quotes arround the array key that you are pointing at (otherwise PHP sees it as constant, not a variable.)

 

Also, you need to be consistant with your error capture - you use or die () in the first page during query execution, but not the second.

 

Neither of these statements are absolutely true. When you are using arrays within a double quoted string you don't actually need to quote the keys separately as PHP doesn't look for CONSTANTS within strings. Of course, it is always best to stick to good habits and quote your non numeric keys. The braces {} are also only a recommendation when dealing with complex variables such as arrays and objects within double quoted strings.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.