$db = new PDO('mysql:host=localhost;dbname=my_database', 'username', 'password'); $stmt = $db->prepare("SELECT * FROM users WHERE username = :username"); $username = $_POST['username']; $stmt->bindParam(':username', $username); $stmt->execute(); $result = $stmt->fetchAll();
$db = new PDO('mysql:host=localhost;dbname=my_database', 'username', 'password'); $stmt = $db->prepare("INSERT INTO users (username, password) VALUES (:username, :password)"); $username = $_POST['username']; $password = password_hash($_POST['password'], PASSWORD_DEFAULT); $stmt->bindParam(':username', $username); $stmt->bindParam(':password', $password); $stmt->execute();In this example, we are preparing a SQL query to insert a new record into the users table with a username and password. We are using the `password_hash()` function to hash the password before inserting it into the database. We are using `bindParam()` to bind the values of the username and password parameters. The PDO library is a package library in PHP that provides a consistent interface for accessing databases. It supports multiple database types including MySQL, PostgreSQL, and SQLite. It has features like prepared statements, transactions, and error handling that make it a popular choice for database programming in PHP.