How can PHP be integrated with SQL databases to store user registration data?
To store user registration data in an SQL database using PHP, you can establish a connection to the database, create a table to store the user information, and then insert the user's data into the table using SQL queries.
// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database_name";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// Create a table to store user registration data
$sql = "CREATE TABLE users (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(30) NOT NULL,
email VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
)";
$conn->query($sql);
// Insert user registration data into the table
$username = "example_user";
$email = "user@example.com";
$password = password_hash("password123", PASSWORD_DEFAULT);
$sql = "INSERT INTO users (username, email, password) VALUES ('$username', '$email', '$password')";
$conn->query($sql);
// Close the database connection
$conn->close();
Related Questions
- What are the differences between using sessions and cookies in PHP for storing user data?
- How can developers effectively utilize the Smarty Template System in PHP projects?
- In the context of PHP development, what are the recommended approaches for debugging and resolving server-side issues related to script execution?