What security measures should be implemented when fetching and displaying database data in a PHP ticker?

When fetching and displaying database data in a PHP ticker, it is important to implement security measures to prevent SQL injection attacks. One way to do this is by using prepared statements with parameterized queries to sanitize user input and prevent malicious code execution.

// 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);
}

// Fetch data from the database using a prepared statement
$stmt = $conn->prepare("SELECT * FROM table WHERE id = ?");
$stmt->bind_param("i", $id);

$id = $_GET['id']; // assuming 'id' is passed as a parameter

$stmt->execute();
$result = $stmt->get_result();

// Display the fetched data in the ticker
while ($row = $result->fetch_assoc()) {
    echo $row['column_name'];
}

// Close the connection
$stmt->close();
$conn->close();