MySQL函数给出错误的值

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!

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 .

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";

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);
            }

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

}