connect html to php code example
Example 1: php sql connection
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn= mysqli_connect($servername,$username,$password,$dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected Successfully.";
?>
Example 2: php connect to mysql
$servername = "localhost";
$username = "username";
$password = "password";
$conn = new mysqli($servername, $username, $password);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
Simplified
$conn = mysqli_connect('localhost', 'username', 'password');
$database = mysqli_select_db($conn, 'database');
Example 3: load php in html
//Safe as:
index.html
number.php
//HTML:
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Phone Number</h1>
<?php include('number.php') ?>
</body>
</html>
//PHP:
<?php
echo 4895553268;
?>
Example 4: insert an html page into php
<?php
include('fileOne.html');
include('fileTwo.html');
?>