Simple Name Simple Name - 3 months ago 11
MySQL Question

PHP MYSQLI Get Rows But String With Comma [delete this]

hello here i have a string with comma and i want to select all rows in database table where is my string

$strings = '#a1 , #2';

$sql_query = mysqli_query($, "SELECT * FROM my_table WHERE hashtag LIKE '%$strings%'");
while($fetch_data = mysqli_fetch_array($sql_query)) {
echo $fetch_data['hashtag']. '</br>';
}


my query show me blank...

i want to show
example:

#a1
#a1
#a2
#a2
#a2
#a2


enter image description here

Answer

Commas don't have any special meaning in LIKE patterns, it won't look for matches of either tag. You need to split up the strings and search for each of them with IN.

$strings = '#a1 , #2';
$tags = array_map('trim', explode(',', $strings));
$tags_string = "'" . implode("', '", $tags) . "'";
$sql_query = mysqli_query($, "SELECT * FROM my_table WHERE hashtag IN ($tags_string)");