Forum Moderators: coopster

Message Too Old, No Replies

Echo message if count equals 0

         

ayushchd

7:07 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



Hi again, I just cant get through these silly ones today...

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

ayushchd

7:18 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



I found a way.. it may not be a perfect one...but it does give the desired output..

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

cameraman

7:37 pm on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That looks fine, but how about a structure that's maybe a little simpler:

$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?

ayushchd

7:44 pm on Sep 30, 2007 (gmt 0)

10+ Year Member



Thanks

cameraman

11:47 pm on Sep 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're welcome

dreamcatcher

7:48 am on Oct 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ayushchd, you might want to be a little more descriptive with your subject headings. Future visitors might find your thread useful, so a suitable subject heading would be more appealing.

dc

ayushchd

9:26 am on Oct 1, 2007 (gmt 0)

10+ Year Member



im sorry...i shall take care of this in the future