$dsn = 'mysql:host=localhost;dbname=mydatabase'; $username = 'username'; $password = 'password'; $db = new PDO($dsn, $username, $password);
$statement = $db->prepare("INSERT INTO users (username, password) VALUES (:username, :password)"); $statement->bindParam(':username', $username); $statement->bindParam(':password', $password); $username = 'john'; $password = 'doe'; $statement->execute();
$statement = $db->query("SELECT * FROM users"); $users = $statement->fetchAll(PDO::FETCH_ASSOC); foreach ($users as $user) { echo $user['username'] . "\n"; }This code retrieves all users from a table named 'users' and prints their usernames. The fetchAll() method returns an array containing all the rows from the result set, and the PDO::FETCH_ASSOC option specifies that the array should be indexed by column names rather than numeric indices. In conclusion, PHP ObjectModel is a part of the PDO extension and provides a powerful and efficient way to handle database operations using an object-oriented paradigm.