What are some best practices for formatting and searching data from a MySQL database using PHP?

When formatting and searching data from a MySQL database using PHP, it is important to properly sanitize user input to prevent SQL injection attacks. Additionally, using prepared statements can help improve performance and security. Lastly, utilizing functions like mysqli_real_escape_string() can help escape special characters in the input data.

// Example of formatting and searching data from a MySQL database using PHP

// Establish a connection to the MySQL database
$connection = mysqli_connect("localhost", "username", "password", "database");

// Sanitize user input to prevent SQL injection
$search_term = mysqli_real_escape_string($connection, $_POST['search_term']);

// Prepare a SQL query using a prepared statement
$query = $connection->prepare("SELECT * FROM table WHERE column LIKE ?");
$query->bind_param("s", $search_term);

// Execute the query
$query->execute();

// Fetch results
$result = $query->get_result();

// Loop through the results and display them
while ($row = $result->fetch_assoc()) {
    echo $row['column_name'] . "<br>";
}

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