How can you query a database to retrieve records where a specific string appears in a field?

To retrieve records where a specific string appears in a field, you can use a SQL query with the "LIKE" keyword. This allows you to search for a specific substring within a field. You can use the "%" wildcard to match any sequence of characters before or after the specified string.

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

// Query to retrieve records where a specific string appears in a field
$search_string = "example";
$sql = "SELECT * FROM table_name WHERE field_name LIKE '%$search_string%'";

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // Output data of each row
    while($row = $result->fetch_assoc()) {
        echo "Field value: " . $row["field_name"] . "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();
?>