Page 1 of 1

how to add one more criteria to this?

Posted: Thu Mar 17, 2011 2:02 pm
by someguyhere

Code: Select all

$query = "SELECT * FROM wp_network_members WHERE ". implode(' AND ', $searchSql);
I want to add an AND statement to this so that it also checks if the field wp_posts contains a value of "published"

Re: how to add one more criteria to this?

Posted: Thu Mar 17, 2011 2:15 pm
by divedj
Try the following

Code: Select all

$query = "SELECT * FROM wp_network_members WHERE something = '".$yourVar."' AND wp_posts = 'published'";

Re: how to add one more criteria to this?

Posted: Thu Mar 17, 2011 2:47 pm
by someguyhere
I've tried a few different variations of that, but can't get it to work.

If it makes a difference, the value in $searchSql is an array (with 7 potential elements), which the implode breaks out into a string, for example:

Code: Select all

Array
(
    [0] => 1=1
    [1] => `l_name` LIKE '%Brooks%'
)
Any other recommendations?

Re: how to add one more criteria to this?

Posted: Fri Mar 18, 2011 10:00 am
by divedj
As far as I can see, you are not finishing your WHERE clause. Your query string ends up with ...WHERE AND....

your implode(' AND ', $searchSql) should work after defining a column and a value after WHERE.

Try and print your implode(' AND ', $searchSql) statement to see if it gives you a valid string for your sql query. You might need to adjust the array contents.

The querystring after implode('AND', $searchSQL) has to look like this:

Code: Select all

$res = mysql_query("SELECT * wp_network_members
                    WHERE column1 LIKE '%name%' AND column2 LIKE '%anothername%' AND column3 LIKE '%anothername%' AND wp_posts = 'published'");
That's what your search string has to look like after IMPLODE:

column1 LIKE '%name%' AND column2 LIKE '%anothername%' AND column3 LIKE '%anothername%' ... carrying on for your 7 elements.

Re: how to add one more criteria to this?

Posted: Fri Mar 18, 2011 11:44 am
by miki
try this way

Code: Select all

$type = 1;

$cond = array();
$cond[] = "column1 LIKE '%name1%'";
$cond[] = "column2 LIKE '%name2%'";
$cond[] = "column3 LIKE '%name3%'";

if(1 == $type)
{
    $cond[] = "wp_posts = 'published'";
}

$condition = !empty($cond) ? "where ".implode(" and ", $cond) : '';
$sql = "SELECT * wp_network_members {$condition}";
$res = mysql_query($sql);