$db = new PDO("mysql:host=localhost;dbname=mydatabase", "username", "password"); $stmt = $db->prepare("SELECT * FROM users WHERE id = ?"); $stmt->execute([$userId]); $row = $stmt->fetch(PDO::FETCH_ASSOC); // Accessing the field values using the column names echo "Name: " . $row['name'] . "
"; echo "Email: " . $row['email'] . "
"; echo "Age: " . $row['age'] . "
";
$db = new mysqli("localhost", "username", "password", "mydatabase"); $stmt = $db->prepare("SELECT * FROM products WHERE id = ?"); $stmt->bind_param("i", $productId); $stmt->execute(); $result = $stmt->get_result(); $row = $result->fetch_assoc(); // Accessing the field values using the column names echo "Product Name: " . $row['name'] . "In this example, we are using the mysqli package library to get a single row from the "products" table based on the ID of the product. We bind the parameter and execute the query before retrieving the result with get_result. We then access the field values using the column names and output them to the browser.
"; echo "Price: " . $row['price'] . "
"; echo "Category: " . $row['category'] . "
";