Hello, This might be an easy solution for you guys, but I'm learning php on my own and I'm having problems with this code. Its giving me an output of what I want but I'm also getting this error "Notice: Undefined variable: construct in C:\wamp\www\search_exp\data.php on line 27" the line 27 is on the first $construct line.




$product_search = mysql_real_escape_string($_POST['product_search']);

//$result = mysql_query("SELECT product FROM searchengine WHERE product='$product_search'");
//$result_num_rows = mysql_num_rows($result); //this checks for fales (0) or true (1).
	if ($product_search==NULL) {
    echo "Please enter an ingredient or dish"; }
			echo "You searched for <b>$product_search</b><hr size='1'>";
			$search_exploded = explode(" ", $product_search);
			$x = 0;
			foreach($search_exploded as $search_each) {
        	//construct query
			if ($x==1) {
			$construct .= "product LIKE '%" .$search_each. "%'";}
			$construct .= "OR product LIKE '%" .$search_each. "%'";}
			//	}
//$product_search = 0;
//while ($product_search < count($search_exploded)) {
/*if ($product_search == 0) {
	$construct = "product LIKE '%" . $product_search . "%'";
	} else {
	$construct .= "OR product LIKE '%" . $product_search . "%'";
}*/	}
$construct = "SELECT * FROM searchengine WHERE $construct";  
$run = mysql_query($construct);
$found = mysql_num_rows($run);

if ($found==0)
echo "Sorry, cannot find ingredient or dish. If you like to contribute, you can submit an ingredient or dish that you think will be useful";
	echo "$found results found!<p>";                    
	while ($runrows = mysql_fetch_assoc($run))
		//get data
		$store = $runrows['store'];
		$product = $runrows['product'];
		$description = $runrows['description'];
		//$price = $runrows['price'];
		//$saving = $runrows['saving'];
		//$distance = $runrows['distance'];
		echo "
		Product: $product<br>
		Description: $description<p>";


Recommended Answers

All 8 Replies

You are adding to the string $construct, even though it has not yet been initialized. If you want to get rid of the notice, add $construct = ''; before the if.

$construct = "SELECT * FROM searchengine WHERE $construct";

Problem is in this line.

I think, $construct(Highlighted) will be not there.

Declare construct before foreach loop. like $construct='';

Hello, thanks for the response!

I added exactly what @rajarajan07 said: $construct="";

but now i'm getting "Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in C:\wamp\www\search_exp\data.php on line 48" and line 48 is here: $found = mysql_num_rows($run);

And then, its not giving me my search results, it comes out saying that "Sorry, cannot find ingredient or dish. If you like to contribute, you can submit an ingredient or dish that you think will be useful"

Also, I'm getting outputs of all my records when I have a space in my search, does anyone know how to fix that solution too?


Maybe you have an error in your query. I think your $contruct variable (the one used in query) is still null ... Try to echo the query to see exactly how it looks

For your second problem, try trim() command

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.