AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Communication Technology

The connection page

Week 11 · Unit 3 Creating Site Pages (4), Topic 1 (end): the page that connects to the database: what a connection needs, reading a connection page, keeping secrets · 3 lessons
1

What a program needs to reach a database

ICT Secondary 2 Term 1, Unit 3, Topic 1: pp. 82–85
GoalName the four facts a program needs to open a connection to a database.
New words
database connection · the open link between a PHP page and the database, needed before any record is read or changedserver name · where the database server runs; localhost while testing on your own computeruser name · the account the page uses to talk to the databasedatabase name · which database on the server to open, here dbdictionary
Explanation

Every action on the data, whether adding, searching, editing or deleting, begins with the same step: opening a connection to the database. A connection needs four facts. The server name says where the database server is; while you test on your own computer it is localhost. The user name says which account the page uses. The password proves that the account is genuine. The database name says which database to open: dbdictionary. Because every page needs the same four facts, we write them once, in connection.php, as you planned in Week 4. In this lesson all values are invented placeholders: the user dict_user and the password a-long-secret do not belong to any real site, and a real password is never printed on a page.

FactExample value (invented)In this project
server namelocalhostthe same computer
user namedict_usera user made for the site
passworda-long-secreta placeholder, never a real one
database namedbdictionarythe project database
Worked examples
Php
1<?php
2$server = "localhost";
3$user = "dict_user";
4$password = "a-long-secret";
5$database = "dbdictionary";
6echo "Connect to $database on $server as $user";
7?>
Output
Connect to dbdictionary on localhost as dict_user
The four facts are stored in four variables. The echo line prints three of them, but never the password.
MistakeWhat goes wrongCheck this fact
wrong database namethe server has no such databasedatabase name
wrong passwordthe account is refusedpassword
server not runningnobody answersserver name and server
Each fact has a different way of going wrong. Karim matches each mistake to the message he would meet and to the fact to check.
Class activity

Write on paper the four facts for an invented games-club database: the server name, the user name, a placeholder password and a database name. Then write the PHP lines that store them in four variables.

Practice
1
Name the four facts needed to open a connection.
2
What is the server name while you test on your own computer?
3
Why is the connection code written once in connection.php?
4
Should a page ever print the password? Explain.