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?
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
0
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
0

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!
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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?
0
chaauCommented:
What is not working? How is your table called? Do you really have a column called string?
0
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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.