Connect MySQL and insert data in PHP
Visual Studio Code
SublimeText
A simple way to connect MySQL and insert data into a database.
<?php
$servername = "localhost"; // Replace with your MySQL server address
$username = "your_username"; // Replace with your MySQL username
$password = "your_password"; // Replace with your MySQL password
$dbname = "your_database"; // Replace with your MySQL database name
// Create a connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check the connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$username = "john_doe";
$email = "[email protected]";
$sql = "INSERT INTO users (username, email) VALUES ('$username', '$email')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
// Close the connection
$conn->close();
?>
Loading...
Sorry! No comment found:(
There is no comment to show for this.
Leave your comment
Tested Versions
- PHP 8