0

How to write mysql where clause inside php foreach loop. here is my code

$outPut = Array ( [0] => 'intel' [1] => 'amd' );
$Query = select * from laptop
foreach($outPut as $Filtervalues)
{
  $Query .= "where processor like '%$Filtervalues%'";
}

echo $Query;

But my output is not correct.

i want output to be as

select * from laptop where (processor like '%intel%' or processor like '%amd%')
6
  • what are you currently getting Commented Sep 27, 2013 at 12:05
  • @Armand i am getting out put as select * from laptop where processor like '%intel%' where processor like '%amd%' Commented Sep 27, 2013 at 12:14
  • @All: variables in php are case sensitive $outPut differs from $output I guess that's another mistake. Commented Sep 27, 2013 at 12:17
  • 1
    try mine answer :) should work: codepad.org/bMrilK67 Commented Sep 27, 2013 at 12:19
  • 1
    @All: his array initialization is wrong, too. Commented Sep 27, 2013 at 12:20

6 Answers 6

4

foreach not needed:

<?
$output = Array ( 'intel', 'amd' );
$Query = echo 'select * from laptop where processor LIKE \'%'.join($output,'%\' OR processor LIKE \'%').'\'%';
?>

Hope this helps

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

1 Comment

if you use quotes properly you can avoid the backslashes and make code more readable.
1

You have to put the WHERE clause only for the first filter parameter:

$output = Array ( 'intel', 'amd' );
$Query = select * from laptop
$firsttime=true;

foreach($output as $Filtervalues)
{
    if($firsttime){
        $Query .= "where";
        $firsttime=false;
    }
    else
        $Query .= "or";

    $Query .= " processor like '%Filtervalues%' ";
}

echo $Query;

Comments

1

try this:

<?php 

$output = Array ( 'intel' , 'amd' );
$Query = 'select * from laptop where ';
foreach($output as $Filtervalues)
{
  $whereQuery .= "processor like '%".$Filtervalues."%' OR ";
}

echo $Query . substr($whereQuery, 0, -3);


?>

WORKING CODE

3 Comments

That will result to error if there's no $filterValues. The where should be added only if $filterValues has a count more than 0
@Nadeem_MK $output = Array ( [0] => 'intel' [1] => 'amd' ); count($output) = 2 -> NOT 0
@Adam ok, but you can't assume it will remain this way. If $outputwas to remain fixed, OP could as well hard code his query.
1

Try the following:

$output = Array ( 'intel', 'amd' );
$Query = "select * from laptop ";

if(count($output)>0){
    $Query .= "WHERE";

    foreach($output as $Filtervalues){
        $Query .= " processor like '%$Filtervalues%' OR";
    }
    $Query = trim($Query, " OR");
}

echo $Query;

Comments

1

Try this

$counter = 0;
$output = Array ( [0] => 'intel' [1] => 'amd' );
$Query = "select * from laptop ";
if(count($output) > 0)
     $Query .= " where ( ";
foreach($outPut as $Filtervalues)
{
    $counter++;
    $Query .= "processor like '%$Filtervalues%'";
    if($counter != count($output))
        $Query .= " or ";
    else
        $Query .= ")";
}

echo $Query;

6 Comments

That will produce several where! Only 1 where should be added before the loop, in case $Filtervalues is not empty
parse erreor in line 3 :)
That will result to error if there's no $filterValues. The where should be added only if $filterValues has a count more than 0
@Nadeem_MK you mean $output.
@Christoph Yeah sorry ;)
|
1

You could add all conditions to an array and just join its elements later.

$output = array ( 'intel', 'amd' );
$Query = 'select * from laptop';
$likes = array();
foreach($output as $Filtervalues)
{
  $likes[] = "processor like '%$Filtervalues%'";
}
if(!empty($likes))
    $Query .= ' where ('.implode(' or ', $likes) . ')';
echo $Query;

1 Comment

@Christoph: Thanks for the edit. I prematurely submitted and was doing the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.