Display MySQL data as a link

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smileboy
Forum Newbie
Posts: 20
Joined: Wed Mar 11, 2009 6:58 am
Location: Tashkent, Uzbekistan

Display MySQL data as a link

Post 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
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Display MySQL data as a link

Post 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.
smileboy
Forum Newbie
Posts: 20
Joined: Wed Mar 11, 2009 6:58 am
Location: Tashkent, Uzbekistan

Re: Display MySQL data as a link

Post 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.
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Display MySQL data as a link

Post by Paul Arnold »

Have you selected your database?

mysql_select_db();
smileboy
Forum Newbie
Posts: 20
Joined: Wed Mar 11, 2009 6:58 am
Location: Tashkent, Uzbekistan

Re: Display MySQL data as a link

Post 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.
Paul Arnold
Forum Contributor
Posts: 141
Joined: Fri Jun 13, 2008 10:09 am
Location: Newcastle Upon Tyne

Re: Display MySQL data as a link

Post 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 />
smileboy
Forum Newbie
Posts: 20
Joined: Wed Mar 11, 2009 6:58 am
Location: Tashkent, Uzbekistan

Re: Display MySQL data as a link

Post 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.
Post Reply