$db = new PDO('mysql:host=localhost;dbname=mydb;charset=utf8', 'username', 'password'); $query = "SELECT * FROM users WHERE id = ?"; $stmt = $db->prepare($query); $stmt->execute(array(1)); $user = $stmt->fetch(PDO::FETCH_ASSOC); // Retrieving data echo $user['name']; echo $user['email'];
$mysqli = new mysqli("localhost", "my_user", "my_password", "mydb"); $query = "SELECT * FROM users WHERE id = ?"; $stmt = $mysqli->prepare($query); $stmt->bind_param("i", $id); $id = 1; $stmt->execute(); $result = $stmt->get_result(); $user = $result->fetch_assoc(); // Retrieving data echo $user['name']; echo $user['email'];In this example, we are using mysqli to query a database and retrieve a single row of user data where id is equal to 1. We are then fetching the result array using the `fetch_assoc()` method and using the data retrieved to display the name and email of the user. The `queryOne()` method is commonly found in PHP database libraries such as PDO and mysqli.