// Establish a database connection using PDO $pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password'); // Prepare a query to retrieve a row from the users table $stmt = $pdo->prepare('SELECT * FROM users WHERE id = :id'); // Bind the ID parameter to the query $stmt->bindParam(':id', $id); // Execute the query $stmt->execute(); // Fetch the row as an associative array $row = $stmt->fetch(PDO::FETCH_ASSOC); // Echo out the row data echo $row['id'] . ' ' . $row['name'] . ' ' . $row['email'];
// Establish a database connection using mysqli $conn = mysqli_connect('localhost', 'username', 'password', 'mydatabase'); // Prepare a query to retrieve a row from the users table $query = "SELECT * FROM users WHERE id = $id"; // Execute the query and fetch a single row $result = mysqli_query($conn, $query); $row = mysqli_fetch_assoc($result); // Echo out the row data echo $row['id'] . ' ' . $row['name'] . ' ' . $row['email'];In this example, we use the mysqli extension to connect to a MySQL database and retrieve a single row from the "users" table based on the ID parameter passed to the query. We then fetch the row as an associative array and echo out the data. Package library: mysqli