0

I am creating a search engine whereby I require videos to be displayed according to the keywords input.

So for my codes, I have

$search_exploded = explode (" ", $search);
            foreach($search_exploded as $search_each){
                $x = 0; 
                $x++;

                if($x>=1){
                    $construct ="keywords LIKE '%$search_each%'";
                }

                else{
                    $construct ="OR keywords LIKE '%$search_each%'";

                }
                $x = 0;
            }

and

$query ="SELECT * FROM test WHERE $construct";

                    $runquery = mysql_query($query);

                    $foundnum = mysql_num_rows($runquery);

The problem lies in the $runquery as my the error i get from my browser states that the line $foundnum = mysql_num_rows($runquery); is returning a Boolean value instead of the supposed resource type value.

Can anyone help fix this? I'm stuck on this for quite some time now. Thankful for and appreciate any help!

1
  • You have a issue in your foreach.. You are resetting it each time you run through it, meaning it will always be 1. - also, your $construct is reset to the latest value each time it's run through the loop Commented Nov 10, 2015 at 8:27

4 Answers 4

2

there is a problem in if condition and every time you set $x to 0 , then why you init it.

   $x = 0;
  foreach($search_exploded as $search_each){               
           if($x==0){ 
                $construct =" keywords LIKE '%$search_each%' ";
            }else{
                $construct .=" OR keywords LIKE '%$search_each%' ";
            }
            $x++;
        }

Try this .

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

3 Comments

$construct .="OR..." maybe no ?
yes, $construct .=" OR keywords LIKE '%$search_each%' ";
Just a small thing if you care, but there is no need for spaces in both ends of the $construct ends, it's enough with one in either end.
2

You have a couple of logical errors inside your foreach loop pertaining to the $x variable.

Here is a simple way to achieve what you are trying to do (without using some kind of flags like $x)-

$search_exploded = explode (" ", $search);

// An array for the `LIKE` conditions
$construct_list = [];

// Adding the conditions in the array
foreach($search_exploded as $search_each){
    $construct_list[] = "keywords LIKE '%$search_each%'";
}

// Joining them using OR
$construct = implode(" OR ", $construct_list);

// Supposing there are no keywords, the
// WHERE should not exist. So make a separate var for that - 
$where_clause = "";
if(trim($construct) != ""){
    $where_clause = "WHERE $construct";
}

// Perform your query
$query ="SELECT * FROM test $where_clause";

1 Comment

Great, this worked out for me! Thanks for your help. :)
0

Try this:

            $search_exploded = explode (" ", $search);
            $construct = '1';
            if (!empty($search_exploded)) {
                $construct = '';
                foreach($search_exploded as $search_each){
                    $construct .= $construct == '' ? " keywords LIKE '%$search_each%'" : " OR keywords LIKE '%$search_each%'";
                }
            }
            $query ="SELECT * FROM test WHERE $construct";

            $runquery = mysql_query($query);
            if ($runquery) {
                $foundnum = mysql_num_rows($runquery);
            }

Comments

0

You seem to try to omit the OR for the 2nd and subsequent runs around the loop, but keep it for the first. should be the other way round,.

But I would probably avoid using the loop, and just use implode. Something like this (although would need to escape the values before using them in the query).

$search_exploded = explode (" ", $search);

if (count($search_exploded) > 0)
{

    $construct = implode("%' OR keywords LIKE '%", $search_exploded);

    $query ="SELECT * FROM test WHERE keywords LIKE '%".$construct."%'";

    $runquery = mysql_query($query);

    $foundnum = mysql_num_rows($runquery);

}

Comments

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.