إعلانيمكن أن تكون علامتك التجارية هناعلامتك التجارية هنافي كل صفحة من Darslab · طلاب وأولياء أمور ومعلمونinfo@darslab.comاسأل عن الأسعارالأسعارinfo@darslab.com
☰ خطة العام · تكنولوجيا المعلومات والاتصالات (بالإنجليزية)

The connection page

الأسبوع 11 · الوحدة ٣ «إنشاء صفحات الموقع» (٤) الموضوع الأول (نهاية): صفحة الاتصال بقاعدة البيانات: ما يحتاجه الاتصال، قراءة صفحة الاتصال، حفظ الأسرار · 3 دروس
2

Reading a connection page, and its errors

ICT Secondary 2 Term 1, Unit 3, Topic 1: pp. 82–85
GoalRead a connection page with try and catch, and show an error without giving secrets away.
New words
mysqli_connect · the PHP function that tries to open a connection: it takes the server, user, password and databaseexception · an error that PHP “throws” so that the program can catch it instead of crashingtry and catch · try runs risky code; catch says what to do if an exception is thrownerror handling · planning what the page does when something goes wrong
Explanation

The book’s connection page used an old family of database functions whose names begin with mysql, then an underscore (not the mysqli family). These were removed from PHP in version 7, and a page that calls them stops with an error. Today we use mysqli_connect, which takes the server, the user, the password and the database. The old logic was “if the connection worked, go on; else print not connected”. But in PHP 8.1 and later a failed mysqli_connect does not return false: it throws an exception, so an if and else would never reach the else. The right shape is try and catch. The risky call goes inside try; if it fails, catch runs. Read the page below: when a server is running, it prints connected; when none is running, it prints not connected. When something goes wrong, show the visitor a short, friendly message. Never print the server’s own error text or any detail of the connection: it can give secrets away.

Starttry: open the connectionan exception was thrown?yesnoshow a short messagego on with the pageEnd
Worked examples
Php
1<?php
2try {
3    $connect = mysqli_connect("localhost", "dict_user", "a-long-secret", "dbdictionary");
4    echo "connected";
5} catch (mysqli_sql_exception $e) {
6    echo "not connected";
7}
8?>
Output
not connected
This connection page was run on a computer where no database server is running. The call throws an exception, catch runs, and the page prints not connected. If a server were running and the four facts were right, it would print connected.
Php
1<?php
2try {
3    $connect = mysqli_connect("localhost", "dict_user", "a-long-secret", "dbdictionary");
4} catch (mysqli_sql_exception $e) {
5    echo "The dictionary is not available now. Please try again later.";
6}
7?>
Output
The dictionary is not available now. Please try again later.
A better catch for visitors. It does not print the server’s error text, only a friendly sentence. The details can be kept for the builder in a private log, never on the page.
Class activity

On paper, trace the page under “try and catch” when the password is wrong: which line runs first, what is thrown, which block runs, and what the visitor sees. Then write one friendly error sentence of your own for the visitor.

Practice
1
Why must we not use the old mysql family of functions that the book’s page used?
2
What does a failed mysqli_connect do in PHP 8.1 and later?
3
Why does if … else not work to detect that failure?
4
What should a visitor see when the connection fails?