How can the LIKE operator in MySQL be utilized effectively in PHP code for search queries?

When using the LIKE operator in MySQL for search queries in PHP, it is important to properly structure the SQL query by concatenating the search term with wildcards (%) to ensure accurate results. This can be achieved by dynamically building the SQL query string within the PHP code using prepared statements to prevent SQL injection vulnerabilities.

// Assuming $searchTerm contains the user input for the search query

// Establish a database connection
$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);
}

// Prepare the SQL query with a placeholder for the search term
$stmt = $conn->prepare("SELECT * FROM table_name WHERE column_name LIKE ?");
$searchTerm = '%' . $searchTerm . '%';
$stmt->bind_param("s", $searchTerm);

// Execute the query
$stmt->execute();
$result = $stmt->get_result();

// Fetch and display the results
while ($row = $result->fetch_assoc()) {
    echo "Column Value: " . $row['column_name'] . "<br>";
}

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