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:
CREATE TABLE Courses ( number CHAR(10) PRIMARY KEY, name VARCHAR(16) ) ENGINE=InnoDB; INSERT INTO Courses VALUES ( '457', 'E-Commerce' ), ( '351', 'File Processing' ); CREATE TABLE Enrollments ( name VARCHAR(32), course CHAR(10), PRIMARY KEY( name, course ), FOREIGN KEY( course ) REFERENCES Courses( number ) ) ENGINE=InnoDB; INSERT INTO Enrollments VALUES ( 'Bart', '351' ), ( 'Bart, '457' ); INSERT INTO Enrollments SELECT 'Ben', number FROM Courses WHERE name = 'E-Commerce'; |
query
function to execute the SQL SELECT
command:
SELECT E.name student, C.name course FROM Enrollments E, Courses C WHERE E.course LIKE '%$course%' AND E.course = C.number;
query
function in the $result
variable.
fetch_assoc
function to return the first row from the recordset
as an array.
fetch_assoc
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']
).