$pdo = new PDO("mysql:host=localhost;dbname=mydatabase", "myusername", "mypassword"); $stmt = $pdo->prepare("INSERT INTO users (name, email) VALUES (:name, :email)"); $stmt->execute(array( ':name' => 'John Doe', ':email' => 'johndoe@example.com' ));
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?"); $stmt->execute(array(1)); $user = $stmt->fetch(PDO::FETCH_ASSOC); echo $user['name'];In this example, we are selecting a user from a table called "users" using a prepared statement with a positional placeholder. The execute function is called with an array containing the value to replace the placeholder. The fetch function is used to retrieve the result set, and we print the user's name. The PDO extension is a core extension in PHP, so no external package or library is required.