Johnnyboy123 Posted May 4, 2011 Share Posted May 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/235491-displaying-2-linked-tables/ Share on other sites More sharing options...
JKG Posted May 4, 2011 Share Posted May 4, 2011 <?php $q = "SELECT * FROM student WHERE cname ='".$_GET['cname']."'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/235491-displaying-2-linked-tables/#findComment-1210313 Share on other sites More sharing options...
Muddy_Funster Posted May 4, 2011 Share Posted May 4, 2011 $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. Quote Link to comment https://forums.phpfreaks.com/topic/235491-displaying-2-linked-tables/#findComment-1210317 Share on other sites More sharing options...
JKG Posted May 4, 2011 Share Posted May 4, 2011 $q = "SELECT RegisteredUsers FROM student WHERE cname = {$_GET[cname]}"; is that quicker or less draining on resources or just less code? Quote Link to comment https://forums.phpfreaks.com/topic/235491-displaying-2-linked-tables/#findComment-1210318 Share on other sites More sharing options...
Muddy_Funster Posted May 4, 2011 Share Posted May 4, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/235491-displaying-2-linked-tables/#findComment-1210322 Share on other sites More sharing options...
trq Posted May 4, 2011 Share Posted May 4, 2011 $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. Quote Link to comment https://forums.phpfreaks.com/topic/235491-displaying-2-linked-tables/#findComment-1210325 Share on other sites More sharing options...
Johnnyboy123 Posted May 4, 2011 Author Share Posted May 4, 2011 Thanks guys, learning some helpful tips. Got my pages sorting and it's displaying fine. Quote Link to comment https://forums.phpfreaks.com/topic/235491-displaying-2-linked-tables/#findComment-1210382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.