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
https://forums.phpfreaks.com/topic/184321-arrays-in-the-sql-string/
Share on other sites

You have to structure the data for the query, do you want to find ALL data in the array or just some data? A good tutorial that may help you out is the Simple SQL Search

 

Which explains how to set it up etc and will hopefully get you going on your way.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.