What are some recommended resources for learning about SQL UNION in PHP?
When working with SQL UNION in PHP, it is important to understand how to combine the results of multiple SELECT statements. One recommended resource for learning about SQL UNION in PHP is the official PHP documentation, which provides detailed explanations and examples. Additionally, online tutorials and forums like Stack Overflow can also be helpful for gaining insights and troubleshooting any issues.
<?php
// Establish a connection to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "database";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query using UNION
$sql = "(SELECT column1 FROM table1) UNION (SELECT column2 FROM table2)";
// Execute the query
$result = $conn->query($sql);
// Output the results
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Column: " . $row["column1"]. "<br>";
}
} else {
echo "0 results";
}
// Close the connection
$conn->close();
?>
Keywords
Related Questions
- What are common mistakes beginners make when trying to run PHP code in a web browser using XAMPP?
- How can PHP developers optimize their code to prevent layout disruptions when using JavaScript for dynamic content display?
- How can PHP's file functions be utilized to read and write data to a text file from a form submission?