When developing an authentication system in PHP, you typically need to store user credentials such as usernames and passwords securely in a database. Below is an example demonstrating how to handle user registration and store the results in a MySQL database using PHP's PDO extension.
<?php
// Database connection settings
$host = 'localhost';
$db = 'your_database_name';
$user = 'your_username';
$pass = 'your_password';
try {
// Create a new PDO instance
$pdo = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// Example user data
$username = 'exampleUser';
$password = 'examplePassword';
// Hash the password
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
// Prepare an SQL statement
$stmt = $pdo->prepare("INSERT INTO users (username, password) VALUES (:username, :password)");
// Bind parameters
$stmt->bindParam(':username', $username);
$stmt->bindParam(':password', $hashedPassword);
// Execute the statement
$stmt->execute();
echo 'User registered successfully!';
} catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
?>
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?