How can the FIND_IN_SET function in MySQL be used to compare a string with a string column in PHP?

To use the FIND_IN_SET function in MySQL to compare a string with a string column in PHP, you can construct a SQL query that includes the FIND_IN_SET function and pass the string value as a parameter. This function will return the position of the string within the comma-separated values in the column, allowing you to check if the string exists in the column.

<?php

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

// String to compare
$string = "example";

// SQL query using FIND_IN_SET function
$sql = "SELECT * FROM table_name WHERE FIND_IN_SET('$string', column_name) > 0";

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

if ($result->num_rows > 0) {
    while($row = $result->fetch_assoc()) {
        // Output data
        echo "ID: " . $row["id"]. " - Name: " . $row["name"]. "<br>";
    }
} else {
    echo "0 results";
}

$conn->close();

?>