-2

I have little question. I have imploded array ($imploded_arr) . I want use it in SQL condition with next variable ($godz). It looks something like that:

r3.meta_key LIKE '$imploded%$godz' . 

But it not works. Probably something wrong is with "commas" . Here is code how looks like my imploded_arr:

$imploded_arr = implode("','", $abc);

Is it posible to do something like this? Thanks advance

I fix it in other way maybe it will help someone: imploded = implode("|", $abc); r3.meta_key LIKE 'location%$godz' AND r3.meta_key REGEXP '$imploded'

Derizen
  • 1
  • 5
  • Can you share the whole query and the database system you use? Is there any error message given? Probably, you want to use a better system for quoting arbitrary input than `implode` – Nico Haase Jul 20 '18 at 12:42
  • Is your title not looking weird to you when you review your question again before submitting in all uppercase – Xatenev Jul 20 '18 at 12:43
  • 1
    @Xatenev Yup you have right :) . I write this post in hurry. I will edit title to less wierd . Best wishes – Derizen Jul 21 '18 at 08:57

1 Answers1

0

A simple implode can be done like this:

$imploded_arr = implode(',', $abc);

That means that you implode the array values and you "glue" them with coma. If you want the combination single-quote/comma/single-quote you should use yours. The problem is in your query. Like does not work like this. The percentage symbol (wildcard) goes either in the beginning or at the end or both.

So if you want to use LIKE in your query, as it is not really clear what you want to do i see a few options.

Either in your php code you make a new string which concatenated your values like:

$NewValue=$imploded_arr.$godz 

And then you try

r3.meta_key LIKE '%$NewValue%'

Also in your example i see that you are using a wrong variable name inside like check this out as well. You are using $imploded instead of $imploded_arr

Another option is to use two like and then handle the results somethlike like this:

 r3.meta_key LIKE '$imploded_arr%' and  r3.meta_key LIKE '%$godz'

That means that you will check for values that start with the value of your $imploded_arr string and end with the value of the $godz string.

Those are a few options i can see but if you provide more info like whole query or a bigger part of your code we can be more specific.

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
  • Hi, Sorry for my delay. I use ACF repeater field. So r3.meta_key looks like 'repeater_0_repeater_sub_field' . $imploded is good variable cuz, `$imploded = (str_replace("_lokacja", "", $imploded_arr));` . $imploded give me name of repeater . "Wildcard" give me number of field. $godz give me name of repeater_sub_filed. So it should looks like `r3.meta_key LIKE $imploded%$godz` . – Derizen Jul 21 '18 at 08:37
  • Here is my query: `$przyjazd = $wpdb->get_results("SELECT r1.post_id as id1,r1.meta_key as min,r1.meta_value as odjazd ,r2.meta_value as przyjazd, r2.meta_key as max, r3.meta_key as godziny, r4.meta_key as godzprzyj FROM wp_postmeta r1 INNER JOIN wp_postmeta r2 on r2.post_id = r1.post_id INNER JOIN wp_postmeta r3 on r3.post_id = r1.post_id WHERE r1.meta_value = '$od' AND r2.meta_value = '$do' AND r1.meta_key IN ('$imploded_arr') AND r2.meta_key IN ('$imploded_arr1') AND r2.meta_key>r1.meta_key AND r3.meta_key LIKE '$imploded'%'$godz'", ARRAY_A)`; – Derizen Jul 21 '18 at 08:38
  • I will try that option: `r3.meta_key LIKE '$imploded_arr%' and r3.meta_key LIKE '%godz'` and I will give anwser soon :) Best wishes :) – Derizen Jul 21 '18 at 08:40
  • Unfortunately that option not work. But that option is the most fit to my question. This is my sql error: `[You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''location_0','location_2' AND r3.meta_key LIKE '%_czas_odjazdu' AND r4.met' at line 7]` Thanks for your time – Derizen Jul 23 '18 at 05:56
  • That's an sql error. Provide your query so we can fix it. In your first query i don't see location_0 and location_2 column so i assume you used another query. – pr1nc3 Jul 23 '18 at 07:04