Jump to content

Arrays in the sql string


magnetica

Recommended Posts

Basically I have an array that contains strings, and I want the mySQL code to search through it in the query string.

 

 

An example of the array would be:

  dogs

  outtakes

  nature etc

 

 

My code so far is:

$sql = "SELECT COUNT(*) FROM table WHERE column LIKE '%$array1%'";

 

Where do I go from here to make the query search each instance in the array against the column value?

 

Thanks

Link to comment
Share on other sites

So you want it to search for this or this or this.  If you do look at the tutorial there is coding in there on how to dynamically setup the query. But this is the gist of it:

 

$array1 = array("dog", "muck", "fun");
if (is_array($array1)) {
    $search = "(column LIKE '%" . implode("%' OR column LIKE '%", $array1) . "%')";

echo $search;
}

 

Just replace column with what your column name actually is. If OR is not what you are after change it to AND.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.