Include: Easy cut and paste for Programmers

In Day 15 and Day 16, we used same code for connecting to MySQL database. We have cut and paste same code two times. Imagine, we done this for all the PHP pages of our application, that will be lot of cut and paste.

What if your MySQL user name or password changed ? You have to go through all the PHP files and change MySQL username and password.

PHP Code:
$db_server '127.0.0.1';
$db_user 'fwhphp_user';
$db_password 'k5BJRaX6SFbs';
$db_name 'fwhphp_db'
Don't repeat yourself (DRY)

One of the programming concept of effective programming is Don't repeat yourself. We can use PHP functions and Includes to accomplish this.

We have not covered functions yet, this is very important when you make large programs. Functions allow you to create your own custom functions, that do a specific task. We will use them in coming days.

For now, lets learn include

include

include allows you to include another PHP program in your PHP program.

If we moved the Database connection related stuff to its own file, for example database.php and include it in all our PHP programs, we don't have to cut and paste MySQL server user name and passwords in each file. This allow us to change the Database login details in one place.

It is good practice to create a folder "includes" and put all your include files in it.

So lets create file

includes/database.php

PHP Code:
<?php

$db_server 
'127.0.0.1';
$db_user 'fwhphp_user';
$db_password 'k5BJRaX6SFbs';
$db_name 'fwhphp_db';

$mysqli = new mysqli($db_server$db_user$db_password$db_name);

if (
$mysqli->connect_errno) {
    echo 
'Connect failed: ' $mysqli->connect_error;
    exit();
}
Now, edit following files

1. signup.php
2. lost_password.php

Find

PHP Code:
$db_server '127.0.0.1';
$db_user 'fwhphp_user';
$db_password 'k5BJRaX6SFbs';
$db_name 'fwhphp_db';

$mysqli = new mysqli($db_server$db_user$db_password$db_name);

if (
$mysqli->connect_errno) {
    echo 
'Connect failed: ' $mysqli->connect_error;
    exit();

Replace it with

PHP Code:
include "includes/database.php"
Now your programs still work. If you ever need to change database login details, just update includes/database.php file.

require

require is same as include.

require is better than include as it will die if includes/database.php file not found. So it is better use require in your programs.

Find

PHP Code:
include "includes/database.php"
Replace With

PHP Code:
require "includes/database.php"
Put all Includes at top

It is good to have all your include/require at top of a program. So you know which other program files are included in your program. So updated lost_password.php file will look like.

PHP Code:
<?php

include "includes/database.php";

if (isset(
$_POST['email'])) {
    
$email $_POST['email'];
} else {
    die(
'Please enter your email address');
}

// now we have $email variable.
// $email can contain space if user enter white space..
// So lets remove any white space char using trim() function.

$email trim($email);

// lets write an SQL statement to search our users table
// to find the user having email address == $email

$sql "select * from users where email='$email'";

// In above line, we used single quotes between email, because it is a string.

// Now time to execute the SQL statement.
$result $mysqli->query($sql);

// lets see how many records found in database
// If num_rows is 0, no user found

if ($result->num_rows == 0) {
    die(
"User with email address <b>$email</b> not found");
}

// lets get the user record from MySQLi result

$userInfo mysqli_fetch_assoc($result);

// $userInfo is an associate array.
// Lets print it.
// Now you can see all information about the user, including password.

echo '<pre>';
print_r($userInfo);

// For real life application, we will sent password to users email
// address instead of printing it to browser.
// But for now, this is fine for us.