Forum Moderators: coopster
The following code would return no results found matching your query for every value of $numrows (since it is inside a while loop).
I want it to echo the message only if $numrows in all the cases is 0 else it should not echo the message.
$query1 = "SELECT * FROM listedcomp where company LIKE '$company%';";
$queryresult= mysql_query($query1);
while($maincomp = mysql_fetch_assoc($queryresult)){
$query = "SELECT * FROM shares where company='". $maincomp['id']."'";
$result = mysql_query($query);
if (!$result) {
echo mysql_error();
}
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
echo "<center>No results found matching your query!";
}
$echo = "Y";
$query1 = "SELECT * FROM listedcomp where company LIKE '$company%';";
$queryresult= mysql_query($query1);
while($maincomp = mysql_fetch_assoc($queryresult)){
$query = "SELECT * FROM shares where company='". $maincomp['id']."'";
$result = mysql_query($query);
if (!$result) {
echo mysql_error();
}
$numrows = mysql_num_rows($result);
if ($numrows == 0) {
if ($echo == "N") {
$echo = "N";
} else {
$echo ="Y";
}
} else {
$echo = "N";
}}
if ($echo == "Y") {
echo "<center>No results found matching your query!</center>";
}
$query = mysql_query($sql);
if(!$query) {
// report error
} // endif error in query
else {
if(mysql_num_rows($query)) {
while($data = mysql_fetch_assoc($query)) {
// display results
} // EndWhile getting data
} // EndIf have data
else {
// echo 'no results'
} // EndElse no data
} // endelse no error in query
A real tough guy might even be tempted to combine those first two lines:
if(($query = mysql_query($sql)) === false) {
See how the result gets assigned in the inner set of parentheses, then gets compared to false in the outer set?