Page 344 - Computer Software Application TP - Volume 1
P. 344
COMPUTER SOFTWARE APPLICATION - CITS
EXERCISE 61 : Connect to MySQL database from PHP
and insert, delete & update data in MySQl database from
webpage
Objectives
At the end of this exercise you shall be able to
• create a MySQL database and table from PHP
• insert and update data in MySQL table from PHP
• delete data in MySQL table from PHP.
Requirements
Tools/Materials
• web browser
• PC/Laptop with latest configuration • apache web server
• operating system: windows 10 or 11 • PHP
• Text editor
• MySQL server
Procedure
TASK 1: Connect to MySQL database from PHP
I Connect to MySQL and create a databse
1 Open the text editor
2 Write the following codes
<html >
<head>
<title> create a databse </title>
</head>
<body>
<?php
$servername = “localhost”; // Change this if MySQL server is on a different host
$username = “your_username”; // Change this to your MySQL username
$password = “your_password”; // Change this to your MySQL 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”;
329
CITS : IT & ITES - Computer Software Application - Exercise 60