$stmt = $db->prepare("SELECT * FROM users WHERE username = ?"); $username = 'john_doe'; $stmt->bind_param("s", $username); $stmt->execute(); $result = $stmt->get_result(); while ($row = $result->fetch_assoc()) { echo $row['username']; } $stmt->close();
$stmt = $pdo->prepare('INSERT INTO employees (name, age, position) VALUES (:name, :age, :position)'); $name = 'John Doe'; $age = 35; $position = 'Manager'; $stmt->bindParam(':name', $name); $stmt->bindParam(':age', $age); $stmt->bindParam(':position', $position); $stmt->execute();In this example, we are inserting a new employee into a table with a name, age, and position. We use the `prepare` function to prepare the SQL statement with named placeholders, and we use the `bindParam` function to bind the values of variables to the named placeholders. We then execute the statement to insert the new employee. This example makes use of the PDO library.