The LIKE
operator uses two wildcard characters. You already know about %
, but there's also _
(which basically means "exactly one of any character").
So you can do the second query like
$append2 ="SELECT * FROM $tablename WHERE {$r['COLUMN_NAME']} LIKE '_%{$substring}%_' union";
and it should only match where there's at least one character before $substring
and one after.