1

I have a string that has a few of URLs separated by a | i.e. http://URL1|http://URL2|...|http://URL#

I am trying to build a string to be used in a query so the result will be:

$query = "OR page_url LIKE '%http://URL1%' OR page_url LIKE '%http://URL2%' OR page_url LIKE '%http://URL3%'"

This string will be added into a query I am preparing

$result = $db->prepare("SELECT * FROM table WHERE column1 = 'abc' :urlquery");
$result->bindValue(':urlquery', $query);

Would this work? Also is this the best way to run the query where I have OR page_url LIKE '% ... %' for every URL?

2
  • 1
    "Would this work? " Shouldn't you try it before posting? Commented Dec 3, 2015 at 19:11
  • 1
    This will not work. SQL expects a variable value where :urlquery is going, and no value is expected there. You'll need to do this by concatenating the strings. Commented Dec 3, 2015 at 19:14

3 Answers 3

1

Separating string should be like this

$array = explode('|',$string);

Then you get an array of all urls. Then you can prepare new string

$query = "OR page_url LIKE '%";
foreach($array as $element){
    $query .= $element."%' OR page_url LIKE '%";
}
$query = rtrim($query," OR page_url LIKE '%");

I think now you have your query created to use it

Sign up to request clarification or add additional context in comments.

3 Comments

Just missing a $query .= "%'"; to close off the last URL
I think that im not rtriming this part of string so it should stay on last one
For some reason it is removing the the %' as well. See for yourself
0

$array = explode('|', $numbers);

now you have all the URLs in this array. So you can use it.

Comments

0

You have to do like this

$urls="'%" . str_replace("|", "%'|'%", $url) . "%'";

to make the url like this:

'%http://URL1%'|'%http://URL2%'|...|http:%'

now split them

$split = explode('|', $urls);

then implode them:

 $query = implode('OR page like ', $split);

1 Comment

Not quite.. This will return URL1 OR page like URL2 OR page like URL3. I need '%URL1%'and I need OR PAGE LIKE to come first

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.