Page 1 of 1

Need help with "If" statement for MySQL search

Posted: Mon May 16, 2005 9:26 am
by mhouldridge
Hi,

I have a search box which takes a form and looks through the database for possible matches. I need an If statement to return an echo that the record was not found. I have attempted it at the bottom, but I get an error.

Here is the code for the search.php script;

Code: Select all

<?
mysql_connect("localhost","",""); 
mysql_select_db("audit"); 

$search=$_POST["search"];

$result = mysql_query("SELECT * FROM dedicated WHERE customer LIKE '%$search%'");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   $asset=$r["asset"];
   $customer=$r["customer"];
  
   //display the row
  
if ($r=mysql_fetch_array ="") {
echo "No records found";
}

else { 
echo "<tr><td><a href='viewall.php?varl=".$asset."'>".$asset."</a></td><td>".$customer."</td></tr> ";
}

?>

Posted: Mon May 16, 2005 9:38 am
by JayBird
this would be a better solution

check to see if there were any results before itterating the results!

Code: Select all

mysql_connect("localhost","",""); 
mysql_select_db("audit"); 

$search=$_POST["search"];

$result = mysql_query("SELECT * FROM dedicated WHERE customer LIKE '%$search%'");

//grab all the content

if (mysql_num_rows($result) == 0) {
	echo "No records found";
} else {
	while($r=mysql_fetch_array($result)) {	
		echo "<tr><td><a href='viewall.php?varl=".$r["asset"]."'>".$r["asset"]."</a></td><td>".$r["customer"]."</td></tr> ";
	}
}

Posted: Mon May 16, 2005 10:32 am
by mhouldridge
Hi,

This is how it reads now;

Code: Select all

<?
mysql_connect("localhost","",""); 
mysql_select_db("audit"); 

$search=$_POST["search"];

$result = mysql_query("SELECT * FROM dedicated WHERE customer LIKE '%$search%'");

//grab all the content
while($r=mysql_fetch_array($result))
{	
   $asset=$r["asset"];
   $customer=$r["customer"];
  
if (mysql_num_rows($result) == 0)
{   echo "No records found";
} else { 
   while($r=mysql_fetch_array($result)) 
   {          
     echo "<tr><td><a href='viewall.php?varl=".$r["asset"]."'>".$r["asset"]."</a></td><td>".$r["customer"]."</td></tr> ";  
	   }
}


?>

i get the error;

Parse error: parse error, unexpected $end in F:\Auditwebsite\search.php on line 80 (the last line)

Any ideas?

Posted: Mon May 16, 2005 10:34 am
by JayBird
re-read my post. I did all the code for you, but you changed it!?!?

Posted: Mon May 16, 2005 11:11 am
by mhouldridge
Ah... sorry,

My mistake.

Thank you!