Can you help with this basic bit of code please? (newbish)
Posted: Tue Jul 26, 2005 10:34 am
Hi, ive only been learning PHP & MySQL for a couple of days and have run into a problem.
I have a database(student_register) with 2 tables(students and courses)
the course on student table is listed by its numerical id. The course table contains the courses names indexed by thier id.
i.e:
courses
id| Name
1 | C#
2 | VB
3 | A+
i have writen a PHP script to print the contents of students to an html table but rather than just print the course id i want it to look up the id on the courses table and print its actual name.
I have read that you cant us database relationships under MySQL.. is this ture?
and does it mean you have to code any kind of relational behavoir in PHP?
Here is my code a few pointers on how to access other tables within the same active database would be handy... every time i try to queary courses in the same section of code i queary students i get an result like: "Resource id 4" (or somthing like that)
At the moment it just prints out the course in the table as its id so how should i queary 'courses.course_name' do i need to change my mysql_select_db, open another connection, etc?
Thanks in advance.
I have a database(student_register) with 2 tables(students and courses)
the course on student table is listed by its numerical id. The course table contains the courses names indexed by thier id.
i.e:
courses
id| Name
1 | C#
2 | VB
3 | A+
i have writen a PHP script to print the contents of students to an html table but rather than just print the course id i want it to look up the id on the courses table and print its actual name.
I have read that you cant us database relationships under MySQL.. is this ture?
and does it mean you have to code any kind of relational behavoir in PHP?
Here is my code a few pointers on how to access other tables within the same active database would be handy... every time i try to queary courses in the same section of code i queary students i get an result like: "Resource id 4" (or somthing like that)
At the moment it just prints out the course in the table as its id so how should i queary 'courses.course_name' do i need to change my mysql_select_db, open another connection, etc?
Thanks in advance.
Code: Select all
<?php
$conn = mysql_connect("localhost", "xxxxx", "xxxxxx");
mysql_select_db("student_register", $conn);
$sql = "SELECT * FROM students";
$result = mysql_query($sql, $conn) or die(mysql_error());
echo"<table border =\"1\" cellpadding =\"4\" cellspacing =\"4\">\n";
echo "<tr>";
echo "<td>Student ID</td>";
echo "<td>Name</td>";
echo "<td>Surname</td>";
echo "<td>Address</td>";
echo "<td>City</td>";
echo "<td>Email</td>";
echo "<td>Phone</td>";
echo "<td>Course</td>";
echo "</tr>";
while ($newArray = mysql_fetch_array($result))
{
$id = $newArray['id'];
$name = $newArray['student_name'];
$surname = $newArray['student_surname'];
$address = $newArray['student_address'];
$email = $newArray['student_email'];
$city = $newArray['student_city'];
$phone = $newArray['student_phone'];
$course = $newArray['student_course'];
$comments = $newArray['student_comments'];
echo "<tr>";
echo "<td>$id</td>";
echo "<td>$name</td>";
echo "<td>$surname</td>";
echo "<td>$address</td>";
echo "<td>$city</td>";
echo "<td>$email</td>";
echo "<td>$phone</td>";
echo "<td>$course</td>";
echo "</tr>";
}
?>