Slide 4.d: PHP variables Slide 4.f: A PHP example (cont.) Home |
SELECT
statement is used to select data from a database.
The next example performs the following tasks according to the selected option:
Enrollments
and Courses
if requested.
Enrollments
and Courses
if requested.
The SQL commands are given below.
Note that MySQL does not perform any check to make sure that course
actually exists in Courses
.
CREATE TABLE Courses ( number char( 10 ) PRIMARY KEY, name varchar( 64 ) ); INSERT INTO Courses VALUES ('457','E-Commerce'), ('532','Programming Languages'); CREATE TABLE Enrollments ( name varchar( 64 ), course char( 10 ), PRIMARY KEY ( name, course ), FOREIGN KEY ( course ) REFERENCES Courses ( number ) ); INSERT INTO Enrollments VALUES ('Bart', '532'), ('Bart, '457'); INSERT INTO Enrollments SELECT 'Ben', number FROM Courses WHERE name = 'Programming Languages';
SELECT
command:
SELECT E.name student, C.name course FROM Enrollments E, Courses C where E.course like '%$course%' and E.course = C.number;
mysql_query( )
function in the $result
variable.
recordset
as an array.
mysql_fetch_array( )
returns the next row in the recordset
.
recordset
.
To print the value of each row, we use the PHP $row
variable ($row['student']
and $row['course']
).