0

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.


Thanks

<?php


mysql_connect("localhost","root","SNIP");//
mysql_select_db("SNIP");

$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"; }
		
		else 
		{
			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
			
			$x++;
			if ($x==1) {
			$construct .= "product LIKE '%" .$search_each. "%'";}
			else{
			$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 . "%'";
	}
$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";
else
{
	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 "
		<b>$store</b><br>
		Product: $product<br>
		Description: $description<p>";
	}

}}
?>

Edited by happygeek: login data removed

6
Contributors
8
Replies
18
Views
7 Years
Discussion Span
Last Post by silviuks
0

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.

0

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


Problem is in this line.

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

0

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"

0

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?

Thanks

0

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

Edited by silviuks: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.