Unit 5 PHP Part 3 (PHP - mySQL)
Unit 5 PHP Part 3 (PHP - mySQL)
DATABASE CONNECTIVITY
What is MySQL?
• A database system used on the web
• A database system that runs on a server
• Ideal for both small and large applications
• Very fast, reliable, and easy to use
• Uses standard SQL
• MySQL compiles on a number of platforms
• Free to download and use
• Developed, distributed, and supported by Oracle Corporation
PHP – MySQL system
• PHP combined with MySQL are cross-platform
• Database Queries
• We can query a database for specific information and have a recordset returned.
The query above selects all the data in the "LastName" column from the "Employees" table.
i. Object – oriented
ii. Procedural
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
// Close connection
$conn->close();
?>
<?php
Create Database $servername = "localhost";
$username = "username";
DB Create $password = "password";
• A database consists of one or more tables.
// Create connection
$conn = new mysqli($servername, $username, $password);
• You will need special CREATE privileges
// Check connection
to create or to delete a MySQL database. if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
• The CREATE DATABASE statement is }
$conn->close();
?>
Create Table
• A database table has its own unique name and consists of columns and rows
• id
CREATE TABLE MyGuests (
• firstname id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
• lastname
lastname VARCHAR(30) NOT NULL,
• email email VARCHAR(50),
reg_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE
• reg_date
CURRENT_TIMESTAMP
)
Create Table
• The data type specifies what type of data the column can hold.
• After the data type, we can specify other optional attributes for each column:
• NOT NULL - Each row must contain a value for that column, null values are not allowed
• DEFAULT value - Set a default value that is added when no other value is passed
• UNSIGNED - Used for number types, limits the stored data to positive numbers and zero
• AUTO INCREMENT - MySQL automatically increases the value of the field by 1 each time a new
record is added
• PRIMARY KEY - Used to uniquely identify the rows in a table. The column with PRIMARY KEY
• Each table should have a primary key column (in this case: the "id" column).
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
Insert data in Table
• After a database and a table have been created, we can start adding data in them.
The INSERT INTO statement is used to add new records to a MySQL table:
• If a column is AUTO_INCREMENT (like the "id" column) or TIMESTAMP with default update of current_timesamp (like
the "reg_date" column), it is no need to be specified in the SQL query; MySQL will automatically add the value.
<?php
$servername = "localhost";
Insert data in Table $username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
Update data in Table
UPDATE table_name
WHERE some_column=some_value
• The WHERE clause in the UPDATE syntax: The WHERE clause specifies which record or records that
should be updated.
$conn->close();
?>
Retrieve data from Table
• The SELECT statement is used to select data from one or more tables:
• Notice the WHERE clause in the DELETE syntax: The WHERE clause specifies which record
// Create connection
$conn = new mysqli($servername, $username, $password,
$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$conn->close();
?>
Title and Content Layout with Chart
Picture with
Caption Layout
Caption
Two Content Layout with Table
• First bullet point here Class Group A Group B
• Second bullet point here
Class 1 82 95
• Third bullet point here
Class 2 76 88
Class 3 84 90
Two Content Layout with SmartArt
• First bullet point here
• Second bullet point here
• Third bullet point here Group Group
A C
Group
B
Add a Slide Title - 1
Add a Slide Title - 2
Add a Slide Title - 3
Add a Slide Title -
4