// Create a PDO object and connect to a database $pdo = new PDO('mysql:host=localhost;dbname=mydb', 'user', 'pass'); // Prepare a statement and bind a value to a parameter $stmt = $pdo->prepare('SELECT * FROM mytable WHERE id = :id'); $stmt->bindParam(':id', $id); // Execute the statement and fetch the results $id = 1; $stmt->execute(); $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
// Begin a transaction $pdo->beginTransaction(); // Prepare an INSERT statement $stmt = $pdo->prepare('INSERT INTO mytable (name, age) VALUES (:name, :age)'); // Bind values to parameters $stmt->bindParam(':name', $name); $stmt->bindParam(':age', $age); // Execute the statement multiple times with different values $name = 'John'; $age = 25; $stmt->execute(); $name = 'Mary'; $age = 30; $stmt->execute(); // Commit the transaction $pdo->commit();The PDOStatement class is part of the PHP Standard Library.