Page 1 of 1
Display MySQL data as a link
Posted: Wed Mar 11, 2009 7:22 am
by smileboy
Hi, i'm having a personal website for practice based on databases and i have some problems on displaying the data.
here is the database info:
database name: practice
table name: exam
in exam table there are fields for
module name, exam type, my marks
what i'm looking for is, i wanna to display all "exam type"s as link. when i click the link, it should show the detailed info about the exam (module name, type of the exam and marks)
would be grateful if you could help me on dis
Re: Display MySQL data as a link
Posted: Wed Mar 11, 2009 9:05 am
by Paul Arnold
Code: Select all
<?PHP
$getExams = "SELECT * FROM exam";
$getExams_q = mysql_query($getExams, $connection);
while($getExams_r = mysql_fetch_assoc($getExams_q)) {
?>
<a href="exam.php?id=<?PHP echo $getExam_r['exam_id']; ?>"><?PHP echo $getExams_r['module name']; ?></a>
<?PHP } ?>
And then on a new page called 'exam.php':
Code: Select all
<?PHP
$getExams = "SELECT * FROM exam WHERE exam_id = '".mysql_real_escape_string($_GET['id'])."'";
$getExams_q = mysql_query($getExams, $connection);
$getExams_r = mysql_fetch_assoc($getExams_q); {
?>
Module Name: <?PHP echo $getExam_r['module name']; ?><br />
Exam Type: <?PHP echo $getExam_r['exam type']; ?><br />
My Marks: <?PHP echo $getExam_r['my marks']; ?><br />
<?PHP } ?>
A couple of things that worry me:
I don't know if you were paraphrasing the table structure but you don't want to have spaces in field name, use underscores or similar
Also
Just going on your field names and brief description of what you want I'm not sure your table is going to be normalised.
I've also included a field 'exam_id' which would be an auto-incremented id number which is the primary key for the table.
Re: Display MySQL data as a link
Posted: Thu Mar 12, 2009 2:07 am
by smileboy
hi Paul, thanks for your reply
as you wrote, i've changed the name of the fields using underscore both in the code and in the database.
however, when i tried the code there was two errors. they were in line 16 and 17
16- $getExams_q = mysql_query($getExams, $connection);
17- while($getExams_r = mysql_fetch_assoc($getExams_q)) {
then i've declared $connection equaling it to mysql_connect("localhost", "username", "password") or die(mysql_error());
after that, error for line 16 disappeared and now there is an error in line 17 saying:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result
in my humble opinion, this is also because of not declaring something
because i don't know what the error is about, i couldn't change it or declare it to anything
if you know the solution to this problem, please could you help.
Re: Display MySQL data as a link
Posted: Thu Mar 12, 2009 4:48 am
by Paul Arnold
Have you selected your database?
mysql_select_db();
Re: Display MySQL data as a link
Posted: Fri Mar 13, 2009 8:12 am
by smileboy
oh, yeah you are right.
i've added the select code and now its working fine.
thanks a lot.
however, when i click the links, it is showing:
Module Name:
Exam Type:
My Marks:
but no marks.
Re: Display MySQL data as a link
Posted: Fri Mar 13, 2009 10:41 am
by Paul Arnold
Aaah sorry, just noticed a typo.
Change these lines
# Module Name: <?PHP echo $getExam_r['module name']; ?><br />
# Exam Type: <?PHP echo $getExam_r['exam type']; ?><br />
# My Marks: <?PHP echo $getExam_r['my marks']; ?><br />
To
# Module Name: <?PHP echo $getExams_r['module name']; ?><br />
# Exam Type: <?PHP echo $getExams_r['exam type']; ?><br />
# My Marks: <?PHP echo $getExams_r['my marks']; ?><br />
Re: Display MySQL data as a link
Posted: Sat Mar 14, 2009 1:26 am
by smileboy
oooh yeah, i had to notice that,
i've changed them in a way you wrote but didn't work.
however, it is working when i wrote'em in one line, like this:
echo "Module Name:".$getExams_r['module_name']."<br />Exam Type:".$getExams_r['exam_type']."<br />My Marks:".$getExams_r['my_marks']."<br />";
i cant figure out what's the difference between the code you gave and this one above,
well doesn't matter the code when everything is working OK.
thanks a lot for your GREAT HELP.