What a program needs to reach a database
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.
| Fact | Example value (invented) | In this project |
|---|---|---|
| server name | localhost | the same computer |
| user name | dict_user | a user made for the site |
| password | a-long-secret | a placeholder, never a real one |
| database name | dbdictionary | the project database |
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?>
Connect to dbdictionary on localhost as dict_user
| Mistake | What goes wrong | Check this fact |
|---|---|---|
| wrong database name | the server has no such database | database name |
| wrong password | the account is refused | password |
| server not running | nobody answers | server name and server |
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.