Jump to content

[SOLVED] state a sentence if query is false


contra10

Recommended Posts

i'm trying to say no results have occured if there is no match to a search

 

my coding is

<?php
// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("registration") or die(mysql_error()); 

//This code runs if the form has been submitted
if(isset($_POST['submit']) and ($_POST['country'])) {

$postuser = "{$_POST['username']}";
$postcountry = "{$_POST['country']}";
$postfemale = "{$_POST['sexf']}";
$postcountry = "{$_POST['country']}";
$postage1 = "{$_POST['age1']}";
$postage2 = "{$_POST['age2']}";

$query = "SELECT * FROM users WHERE country='$postcountry'";
if(isset($_POST['username']) and ($_POST['country']) and ($_POST['submit'])){$query .= "and `username` = '$postuser' ";}
if(isset($_POST['sexf']) and ($_POST['country']) and ($_POST['submit'])){$query .= "and `sex` = '$postfemale' ";}
if(isset($_POST['sexm']) and ($_POST['country']) and ($_POST['submit'])){$query .= "and `sex` = 'male' ";}
if(isset($_POST['age1']) and ($_POST['age2']) and ($_POST['country']) and ($_POST['submit'])){$query .= "and `age` BETWEEN '$postage1' and '$postage2'";}
$result = mysql_query($query);
if($result){
while ($row = mysql_fetch_assoc($result)){

$user = "{$row['id']}";
$userq = "{$row['username']}";
$mycountry = "{$row['country']}";
$sex = "{$row['sex']}";
$city = "{$row['city']}";


echo "<tr>"; 
    echo  "<td width='20%'><a style='text-decoration:none' href='http://localhost/profile/index.php?user=$user'><FONT FACE='ariel' color='#0094f7' SIZE='2'>$userq<br>";
echo "<img src='http://localhost/image/imagereplace.php?id=$user'><br>";
echo "$city, $mycountry $sex</a></FONT></td>";
	echo "<td width='60%'></td>";
	echo"<td width='30%' align='right' valign='top'><a style='text-decoration:none' href='http://localhost/friendrequest/index.php?user=$user'>";
	echo "<FONT FACE='ariel' SIZE='2' color='#0094f7' align='center'><b>Add as Friend</b><br></font></a>";
	echo "<a style='text-decoration:none' href='http://localhost/inbox/sendmessage.php?id=$user'>";
	echo "<FONT FACE='ariel' SIZE='2' color='#0094f7' align='center'><b> Send Message</b></Font></a>";
	echo "</td></tr>";
}
}else{
	echo"no results";
}
}



?>

 

how can i echo "no results" if there is no match

 

add just after the query

 

$num = mysql_num_rows($result);

 

and if there is a result then $num will = 1 or more

 

so then run an if statement

 

if($num >= 1) { 
    echo 'show the profile';
} else {
    echo 'no results were found';
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.