What potential issues can arise when trying to manipulate MySQL query results for display in PHP?

One potential issue that can arise when manipulating MySQL query results for display in PHP is the risk of SQL injection attacks if user input is not properly sanitized. To solve this issue, always use prepared statements or parameterized queries to prevent malicious SQL code from being injected into the query.

// Example of using prepared statements to prevent SQL injection

// Assuming $conn is the MySQL database connection

// User input from a form
$user_input = $_POST['user_input'];

// Prepare a SQL statement with a placeholder for the user input
$stmt = $conn->prepare("SELECT * FROM table_name WHERE column_name = ?");
$stmt->bind_param("s", $user_input);

// Execute the statement
$stmt->execute();

// Get the result set
$result = $stmt->get_result();

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

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