Unit 5 (B)
Unit 5 (B)
Unit 5 (B)
Features
User management
Media Management
Theme System
Extended plugins
Multilingual
Accessories / Requirement
Database − MySQL 5.0 +
Web Server −
WAMP (Windows)
LAMP (Linux)
XAMP (Multi-platform)
MAMP (Macintosh)
Operating System − Cross-platform
Browser Support − IE (Internet Explorer 8+), Firefox, Google
chrome, Safari, Opera
PHP Compatibility − PHP 5.2+
Install wordpress
https://wordpress.org/download/
WordPress requires MySQL database. So create a new empty database
with user/password
Click on Run the install
Step (6) − Enter administrative information.
How to proceed
Create Login
Choose Language
Choose type of website
Choose Domain
Wordpress Dashboard
What is PHP?
PHP is one of the primary server-side programming languages for the web
and is what powers a lot of WordPress.
PHP allows you to do some important things:
❑Save content to a database
❑Read content from a database
❑Write loops and conditional statements
❑Pull in WordPress specific information
❑Pull in files like images, media
❑Can pull in files like images, media and other PHP, CSS and JS files
you’re usually just looking through already written PHP in order to customize
little bits and pieces
HTML and PHP
A PHP file is simply a file that ends with .php. Some PHP files contain only
PHP code and some contain a combination of PHP and HTML code.
Within all of the different types of PHP files, you will find what’s called a
PHP block. When you write PHP you need to write it inside of PHP
blocks otherwise it will display as plain text and not be processed as a
programming language by the server.
A PHP block starts with a “<?php” and ends with a “?>”. Blocks can span
many lines or sometimes appear inline along with other code.
Here is an example of a PHP block with a PHP comment inside of it.
<?php
// Example of a PHP Comment
?>
Output Variable
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple
parameters.";
echo $x ;
?>
Data Type
PHP supports the following data types:
String
Integer
Float (floating point numbers - also called double)
Boolean
Array
Object
NULL
Resource
Constants and Functions
<?php
define("GREETING", "Welcome to W3Schools.com!");
function myTest() {
echo GREETING;
}
myTest();
?>
Control statements
<?php
$t = date("H");
switch (n) {
case label1:
code to be executed if n=label1;
break;
}
while($x <= 5) {
echo "The number is: $x <br>";
$x++;
}
?>
Form
<!DOCTYPE HTML>
<html>
<body>
</body>
</html>
Welcome.php
<html>
<body>
</body>
</html>
Get method
<html>
<body>
</body>
</html>
Get method
<html>
<body>
</body>
</html>
MySQL
MySQL is the most popular database system used with PHP.
The data in a MySQL database are stored in tables. A table is a collection
of related data, and it consists of columns and rows.
What is MySQL?
MySQL is a database system used on the web
MySQL is a database system that runs on a server
MySQL is ideal for both small and large applications
MySQL is very fast, reliable, and easy to use
MySQL uses standard SQL
MySQL compiles on a number of platforms
MySQL is free to download and use
MySQL is developed, distributed, and supported by Oracle Corporation
MySQL is named after co-founder Monty Widenius's daughter: My
MySQL Connect
MySQLi extension (the "i" stands for improved) - only for MySQL
PDO (PHP Data Objects) - for all databases
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Create New MySQL Database
<?php
$servername = "localhost";
$username = "username";
$password = "password";
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create database
$sql = "CREATE DATABASE myDB";
if ($conn->query($sql) === TRUE) {
echo "Database created successfully";
} else {
echo "Error creating database: " . $conn->error;
}
$conn->close();
?>
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDBPDO";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $passw
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";
// use exec() because no results are returned
$conn->exec($sql);
echo "New record created successfully";
} catch(PDOException $e) {
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
?>
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
)";
if (mysqli_query($conn, $sql))
Insert Data
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password,
$dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', '[email protected]')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Select and Display Data
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " .
$row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Delete Data
<?php
$conn->close();
?>
Files inclusion
The include() Function
The require() Function
function writeMessage() {
echo "You are really a nice person, Have a nice time!"; }
/* Calling a PHP Function */
writeMessage();