I dont know exactly what you need, but this will search for last 2 characters of a string and find a column value in your table that matches it, and then echo it. Also make a check to see if string is longer than 2.
$string = 'helloworld';
$newstring = substr($string, -2);
// Connect to database
try {
$db = new PDO('mysql:host=localhost;dbname=DATABASE_NAME', 'root', 'password123');
} catch (PDOException $e) {
echo $e->getMessage()."<br>";
die();
}
// Search for the $newstring in a table
$sql = $db->prepare("SELECT * FROM table_name WHERE column = :newstring");
$query = $sql->execute(array(
":newstring" => $newstring
)
);
foreach ($sql as $row) {
$dbstring = $row['column_name'];
echo $dbstring . '<br>';
}