(MySQLi Object-oriented)
[php]
<!DOCTYPE html>
<html>
<body>
<?php
$HostName = "localhost";
$UserName = "root";
$Password = "password";
$dbname = "accounts";
// Create connection
$connection = new mysqli($HostName, $UserName, $Password, $dbname);
// Check connection
if ($connection->connect_error)
{
die("Connection failed: " . $connection->connect_error);
}
//inserting record
$sql = "INSERT INTO users2 (Username, Password, First, Last)
VALUES ('Adam', 'password', 'Adam', 'Gilchrist')";
if ($connection->query($sql) === TRUE)
{
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . " <br> " . $connection->error;
}
$connection->close();
?>
</body>
</html>
[/php]
The output looks like below in phpmyadmin.
MySQLi Procedural
[php]
<html>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "Student";
// Create connection
$connection = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$connection) {
die("Connection failed: " . mysqli_connect_error());
}
//inserting record
$sql = "INSERT INTO users2 (Username, Password, First, Last)
VALUES ('Adam', 'password', 'Adam', 'Gilchrist')";
if (mysqli_query($connection, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
mysqli_close($connection);
?>
</body>
</html>
[/php]