Page 347 - Computer Software Application TP - Volume 1
P. 347
COMPUTER SOFTWARE APPLICATION - CITS
TASK 2 : Connect to MySQL database from PHP and insert data
1 Open the text editor
2 Write the following codes
<html >
<head>
<title> </title>
</head>
<body>
<?php
// Database connection parameters
$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
$database = “example_db”; // Change this to your database name
// Create connection
$conn = new mysqli($servername, $username, $password, $database);
// Check connection
if ($conn->connect_error) {
die(“Connection failed: “ . $conn->connect_error);
}
$sql = “INSERT INTO persons (first_name, last_name, email) VALUES (‘Peter’, ‘Parker’, ‘peterparker@
mail.com’)”;
if ($conn->query($sql) === TRUE) {
332
CITS : IT & ITES - Computer Software Application - Exercise 61