server services,
e.g., MySQL/MariaDB
|
dynamic content/presentation:
PHP
|
webserver:
e.g., Apache
HTML, CSS, JavaScript
/\
|
\/
client:
web-browser
dynamic content/presentation:
JavaScript
|
client:
web-browser
content:
HTML
presentation:
CSS

1. PHP introduction

PHP: Hypertext Preprocessor, an open source scripting language, are executed on the server:
to create, open, read, write, delete, and close files on the server
to collect form data
to send and receive cookies
to add, delete, modify data in database
to control user-access
to encrypt data

A PHP file has extension ".php" and can contain text, HTML, CCS, JavaScript, and PHP codes.

The result of server-side execution is in client-side language to be returned to the webbrowser.

1.1 Check PHP support

Many webservers support PHP and database. To check whether it does, place a php file (with .php extension) to the server:
<?php
phpinfo();
?>

and use webbrowser to check that file. If the server supports php, it will display information on the php it is supporting.

Return to PHP menu

1.2 Server installation

Install a web server
Install PHP
Install a database, e.g., MySQL

Installation instructions are at: http://php.net/manual/en/install.php

Return to PHP menu

1.3 Trial server for developers

Installation of the following trial server, which lacks security and lacks production capacity, may be used for testing during development.

XAMPP Apache + MariaDB + PHP + Perl

A light-weight test server installs only Apache (webserver), MariaDB/MySDB, and PHP only, without other server tools such as Mercury Mail and FileZilla FTP.

The default location is xampp

The server (with Apache, DB, and PHP) is started or stopped by starting the program: xampp-control.exe

Administrative right is needed for the xampp-control to write to the file xampp-control.ini , else 401 permission error to write will be encountered. Check and correct the rights of the file xampp-control.ini so that the user has write permission to it.

The url for the webbrowser to access the root http://localhost/ which directs to: http://localhost/dashboard/

Return to PHP menu

1.4 Syntax

<?php
// PHP codes
// Single line comment
/* multiple lines
of comments
*/
?>

PHP files end with extension .php

Example:
<?php
echo "<h2>This is a test</h2>";
echo "<p>This is a test paragraph</p>";
echo "This is a string ", "joined ", "two parts.";
?>

This is a test heading

This is a test paragraph

This is a string joined two parts.

All (keyword, class, function), except variable name are NOT case-sensitive.

Return to PHP menu

1.5 variable

consists of alpha-numeric characters and underscore,
start with $ , followed by the name of the variable
a variable name must start with a character or underscore;
variable names are case-sensitive.

A variable is created when one first assigns a value to it.

There was no command to declare a variable, but type declarations were added in PHP 7.

Example:
<?php
$varTemp20 = 20;
$varTemp29 = 29;
echo "<h3>".$varTemp20."</h3>";
echo $varTemp20 + $varTemp29;
?>

20

49

Return to PHP menu

4 MySQL

4.1 MySQL: connect

Example:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
// Create database
$sql = "CREATE DATABASE mydb";
if (mysqli_query($conn, $sql)) {
echo "Database created successfully";
} else {
echo "Error creating database: " . mysqli_error($conn);
}
echo "
";
$dbname = "mydb";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully
";
// sql to create table
$sql = "CREATE TABLE MyGuests (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
)";
if (mysqli_query($conn, $sql)) {
echo "Table MyGuests created successfully";
} else {
echo "Error creating table: " . mysqli_error($conn);
}
echo "
";
// Delete database
$sql = "DROP DATABASE mydb";
if (mysqli_query($conn, $sql)) {
echo "Database deleted successfully";
} else {
echo "Error deleting database: " . mysqli_error($conn);
}
mysqli_close($conn);
?>

Connection failed: Access denied for user 'root'@'localhost' (using password: NO)