Need help with "If" statement for MySQL search

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Need help with "If" statement for MySQL search

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

?>
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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> ";
	}
}
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post 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?
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

re-read my post. I did all the code for you, but you changed it!?!?
User avatar
mhouldridge
Forum Contributor
Posts: 267
Joined: Wed Jan 26, 2005 5:13 am

Post by mhouldridge »

Ah... sorry,

My mistake.

Thank you!
Post Reply