Is it possible to have multiple SELECT statements in a single SQL query in PHP?
Yes, it is possible to have multiple SELECT statements in a single SQL query in PHP. You can use UNION or UNION ALL to combine the results of multiple SELECT statements into a single result set. Make sure that the number of columns and their data types match in all SELECT statements.
<?php
// Establish a connection to the database
$pdo = new PDO('mysql:host=localhost;dbname=mydatabase', 'username', 'password');
// Prepare the SQL query with multiple SELECT statements using UNION
$sql = "SELECT column1 FROM table1
UNION
SELECT column2 FROM table2";
// Execute the query
$stmt = $pdo->query($sql);
// Fetch and display the results
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['column1'] . "<br>";
}
?>
Keywords
Related Questions
- What are some common pitfalls when trying to convert HTML format (divs) with IF statements into a PHP variable?
- How can one troubleshoot and resolve warnings related to file permissions when creating folders with PHP scripts?
- What potential issues can arise when using PHP to interact with a MySQL database for user authentication and data manipulation?