how to add one more criteria to this?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

how to add one more criteria to this?

Post 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"
divedj
Forum Commoner
Posts: 47
Joined: Wed Dec 29, 2010 4:32 am
Location: Malta

Re: how to add one more criteria to this?

Post by divedj »

Try the following

Code: Select all

$query = "SELECT * FROM wp_network_members WHERE something = '".$yourVar."' AND wp_posts = 'published'";
someguyhere
Forum Contributor
Posts: 181
Joined: Sun Jul 27, 2008 3:24 pm

Re: how to add one more criteria to this?

Post 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?
divedj
Forum Commoner
Posts: 47
Joined: Wed Dec 29, 2010 4:32 am
Location: Malta

Re: how to add one more criteria to this?

Post 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.
miki
Forum Newbie
Posts: 7
Joined: Fri Mar 18, 2011 2:32 am

Re: how to add one more criteria to this?

Post 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);
Post Reply