$conn = new PDO("mysql:host=localhost;dbname=myDB", "root", ""); $stmt = $conn->prepare("SELECT * FROM myTable WHERE name=:name AND age=:age"); $stmt->bindParam(":name", $name); $stmt->bindParam(":age", $age); $name = "John"; $age = 25; $stmt->execute(); $result = $stmt->fetchAll(); foreach ($result as $row) { echo $row['name'] . " is " . $row['age'] . " years old.
"; }
$conn = new PDO("mysql:host=localhost;dbname=myDB", "root", ""); $stmt = $conn->prepare("INSERT INTO myTable (name, age) VALUES (:name, :age)"); $stmt->bindParam(":name", $name); $stmt->bindParam(":age", $age); $name = "Jane"; $age = 30; $stmt->execute();In this example, we use PDO prepare to prepare a SQL statement that inserts a row into a database table. We use bindParam to bind the values of $name and $age to the placeholders in the SQL statement. Finally, we execute the statement to insert the row. Conclusion: PDO is a package library in PHP that provides a consistent way to access various databases using a common interface. With PDO prepare, we can prepare SQL statements and bind parameters to placeholders to ensure safe and efficient database operations.