How can the issue of displaying duplicate entries for suppliers in a PHP table be resolved?
Issue: The problem of displaying duplicate entries for suppliers in a PHP table can be resolved by using the DISTINCT keyword in the SQL query to retrieve unique supplier records.
<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// SQL query to retrieve unique supplier records
$sql = "SELECT DISTINCT supplier_name, supplier_address FROM suppliers";
$result = $conn->query($sql);
// Display the unique supplier records in a table
if ($result->num_rows > 0) {
echo "<table><tr><th>Supplier Name</th><th>Supplier Address</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["supplier_name"]."</td><td>".$row["supplier_address"]."</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
Related Questions
- Are there best practices for configuring the headers in the mail() function to ensure successful email delivery?
- What are the potential security risks associated with querying a database using user input in PHP?
- How can PHP developers troubleshoot and resolve encoding issues related to usernames in a multi-language login system?