try { $pdo = new PDO('mysql:host=localhost;dbname=testdb','username','password'); $stmt = $pdo->prepare('SELECT * FROM users WHERE id=:id'); $stmt->execute(['id' => 1]); $result = $stmt->fetch(PDO::FETCH_ASSOC); print_r($result); } catch(PDOException $e) { echo "Error: " . $e->getMessage(); }
try { $pdo = new PDO('pgsql:host=localhost;dbname=testdb','username','password'); $stmt = $pdo->prepare('UPDATE products SET price=:price WHERE id=:id'); $stmt->execute(['price' => 10.99, 'id' => 12]); echo "Product updated successfully!"; } catch(PDOException $e) { echo "Error: " . $e->getMessage(); }In this example, we are connecting to a PostgreSQL database using PDO and updating the price of a product with the ID of 12 in the 'products' table. Overall, PHP Database doQuery is a useful function for interacting with databases in PHP and is commonly used in web development.