mySQLI SELECT LIKE masked compare

Must search in a mysqli DB with 'SELECT * FROM table WHERE String like $string. A '?' in the reference String can represent any character but all the others in the $string must match.
Eg. the reference String ='abcd?efg' would match $stringa = 'abcdXefg' or $stringc = 'abcd1efg'. How does the mySQL "Select..." look like? Is ther a easy way.

$string = 'abcd?efg';    
$row = $mysqli->query("SELECT * FROM $table WHERE String LIKE '$string'");
$result = $row->fetch_assoc();
print_r( json_encode($result) );

Open in new window

SolenthalerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
In MySQL there is a placeholder wild-card that is the percent sign, but I've never seen it embedded.  My first step would be to define what would match and what would not match, and set up a couple of test rows in a separate test table so I could do proof of concept.

Here is what I might do: $sql = "SELECT FROM myTable WHERE myColumn LIKE 'abcd%'";

When you run this query you will get a results set with all of the columns that match 'abcd' follwed by anything.  Then you can use an iterator to walk over the results set and discard the values you do not want.

There might be some kind of regular expression that you could use in the SQL statement, too.  Worth looking that up to see if you can match the pattern easily with REGEX.
https://dev.mysql.com/doc/refman/5.7/en/regexp.html
chaauCommented:
According to MySQL documentation it is the _ (underscore) symbol that matches a single character.
$string = 'abcd_efg';    
$row = $mysqli->query("SELECT * FROM $table WHERE String LIKE '$string'");
$result = $row->fetch_assoc();
print_r( json_encode($result) );

Open in new window

If you are familiar with Regular Expressions you can use REGEXP operator, but it is definitely not easier way

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Yes, @chaau, the underscore notation definitely looks easier!  Good call!
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

SolenthalerAuthor Commented:
Dear experts, did it  with an underscore like chaau proposed. The code above is not doing what i wanted.
Have you ever tried?
chaauCommented:
What is not working? How is your table called? Do you really have a column called string?
SolenthalerAuthor Commented:
Hi all
$string = 'abcd?efg';  
$string = strtr($string, "?", "_");  
$row = $mysqli->query("SELECT * FROM $table WHERE  '$string' LIKE String ");
$result = $row->fetch_assoc();
print_r( json_encode($result) );

Open in new window

After i swaped the position String and $string the code is working.
Thanks to all
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.