How can PHP be utilized to query and manipulate data from multiple database tables for a specific application or project?
To query and manipulate data from multiple database tables in PHP, you can use SQL JOIN statements to combine data from different tables based on a related column. This allows you to retrieve and manipulate data from multiple tables in a single query, making it easier to work with related data in your application.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Query to retrieve data from multiple tables using JOIN
$sql = "SELECT users.username, orders.order_id
FROM users
INNER JOIN orders ON users.user_id = orders.user_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// Output data of each row
while($row = $result->fetch_assoc()) {
echo "Username: " . $row["username"]. " - Order ID: " . $row["order_id"]. "<br>";
}
} else {
echo "0 results";
}
$conn->close();
?>