$dsn = 'mysql:host=localhost;dbname=testdb'; $username = 'username'; $password = 'password'; // Connect to the database using PDO $dbh = new PDO($dsn, $username, $password); // Prepare a SQL statement $stmt = $dbh->prepare("SELECT name, email FROM users WHERE id = :id"); // Bind the parameter $stmt->bindParam(':id', $id); // Execute the statement $stmt->execute(); // Fetch the data as an object $user = $stmt->fetch(PDO::FETCH_OBJ); // Print the user's name and email echo "Name: " . $user->name . "\n"; echo "Email: " . $user->email . "\n";
$xml = simplexml_load_file('data.xml'); // Loop through all the books foreach ($xml->book as $book) { // Fetch the book's name and author as an object $name = $book->name; $author = $book->author; // Print the book's name and author echo "Name: " . $name . "\n"; echo "Author: " . $author . "\n"; }In this example, we are using the SimpleXML library to load an XML file that contains a list of books. We loop through all the books in the file and fetch each book's name and author as an object. We then print out the book's name and author. The packages used in these examples are PDO and SimpleXML libraries, respectively.