0

I suppose to get a string from my database that looks like that:

6841819,6595747,6597673,6696253,6616167,6616197,6611931,6600475,6760303,6748661

Then I need to separate the string using explode() , and to implode again to build a sql query string.
There's my array like this after explode() :

Array
(
    [0] => 6841819
    [1] => 6595747
    [2] => 6597673
    [3] => 6696253
    [4] => 6616167
    [5] => 6616197
    [6] => 6611931
    [7] => 6600475
    [8] => 6760303
    [9] => 6748661
)

And my codes:

$sql = "select * from table where id like %0001% or id like %";
$explode = explode(",", $array);
$implode = implode(" or id like %", $explode);
$sql.= $implodevno."<br>";
echo $sql;

And the output sql query which is incorrect because it missed a '%' at the end of each value:

select * from table where id like %0001% or id like %6841819 or id like %6595747 or id like %6597673 or id like %6696253 or id like %6616167 or id like %6616197 or id like %6611931 or id like %6600475 or id like %6760303 or id like %6748661

I can't find solutions to add a '%' behind using implode(), any solutions or better way to do this ?
The correct output should be:

select * from table where id like %0001% or id like %6841819% or id like %6595747% or id like %6597673% or id like %6696253% or id like %6616167% or id like %6616197% or id like %6611931% or id like %6600475% or id like %6760303% or id like %6748661%

3
  • If you have the possibility to normalize your database so do it immediately.... otherwise you will have alot of issues with the comma separated data Commented Jul 16, 2018 at 8:46
  • @B001ᛦ its actually a very complicated case, that's the only way that i think of to build a query Commented Jul 16, 2018 at 8:48
  • its actually a very complicated case... I think most of the complexity is because of the bad data structure which will make a lot of issues in future as well... Commented Jul 16, 2018 at 8:50

4 Answers 4

3

Simple foreach loop :-

$sql = "select * from table where id like %0001% or ";
$array = Array
        (
            '0' => 6841819,
            '1' => 6595747,
            '2' => 6597673,
            '3' => 6696253,
            '4' => 6616167,
            '5' => 6616197,
            '6' => 6611931,
            '7' => 6600475,
            '8' => 6760303,
            '9' => 6748661,
        );
foreach ($array as $key => $value) {

    $sql .= "id like '%".$value."%' or ";
}
$sql = rtrim($sql,'or ');
echo $sql;

output :-

select * from table where id like %0001% or id like '%6841819%' or id like '%6595747%' or id like '%6597673%' or id like '%6696253%' or id like '%6616167%' or id like '%6616197%' or id like '%6611931%' or id like '%6600475%' or id like '%6760303%' or id like '%6748661%'
Sign up to request clarification or add additional context in comments.

Comments

3

To get your desired result from your current code you could use array_map to add a % before and after each item in your array and then use implode to join them all together:

$sql = "select * from table where id like %0001% or id like %";
$explode = explode(",", $array);
$implode = implode(" or id like ", array_map(function ($item) {
    return '%' . $item . '%';
}, $explode));
$sql.= $implodevno."<br>";
echo $sql;

Take a look a the documentation for array_map here.

It is probably worth mentioning that when doing a LIKE match in MySQL, you should enclose your values within quotes. The array_map function can help you there too. You just need to return '\'%' . $item . '\'%' or "'%$item%'" from the closure.

There are of course more elegant solutions to what you're doing as a whole but that should help you achieve what you're after for now.

Comments

2

If you want to do it exactly as you do, try this:

$array = "6841819,6595747,6597673,6696253,6616167,6616197,6611931,6600475,6760303,6748661";

$sql = "select * FROM `table` WHERE `id` LIKE '%0001%' or `id` LIKE ";

$explode_array = explode(",", $array);
$explode_array = array_map(function($v){return "'%{$v}%'";}, $explode_array);

$implode_str = implode(" or `id` LIKE ", $explode_array);

$sql.= $implode_str."<br>";

echo $sql;

output is:

select * from table where id like %0001% or id like '%6841819%' or id like '%6595747%' or id like '%6597673%' or id like '%6696253%' or id like '%6616167%' or id like '%6616197%' or id like '%6611931%' or id like '%6600475%' or id like '%6760303%' or id like '%6748661%'

Comments

1

Try this

$sql = "select * from table where id like %0001% or id like %";

$n = "6841819,6595747,6597673,6696253,6616167,6616197,6611931,6600475,6760303,6748661";
$n_arr = explode(",", $n);
$n_map = array_map(function($v){ return '%'.$v.'%'; }, $n_arr);
$implode = implode(" or id like ", $n_map);
$sql.= $implode."<br>";
echo $sql;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.