What is the purpose of the script for news output in PHP?

The purpose of the script for news output in PHP is to dynamically display news articles or updates on a website. This script typically retrieves news data from a database or external API and formats it for display on the webpage.

<?php
// Connect to the database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "news_db";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Retrieve news articles from the database
$sql = "SELECT * FROM news_articles";
$result = $conn->query($sql);

// Display news articles on the webpage
if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        echo "<h2>" . $row["title"] . "</h2>";
        echo "<p>" . $row["content"] . "</p>";
        echo "<p><em>Published on: " . $row["publish_date"] . "</em></p>";
    }
} else {
    echo "No news articles found.";
}

$conn->close();
?>