steviemac Posted February 21, 2012 Share Posted February 21, 2012 I am trying make my code display different things depending on how many rows are returned This is my code <?PHP include 'db_con.php'; $month = date("n"); $day = date("j"); $sql = mysql_query("SELECT * FROM table WHERE month = $month AND day = $day ")or die(mysql_error()); while($row = mysql_fetch_array($sql) ) if (mysql_fetch_array($row = 1 ) ) ///THIS IS AN ERROR BUT IF ONE ROW RETURNED THEN THIS { echo "ONE THING"; } elseif (mysql_fetch_array($row >1 ) )////IF > 1 ROW RETURNED THAN THIS { echo"TWO THINGS"; } else { echo "SOMETHING ELSE "; } ?> I have tried several variations of the above code but cannot get it to work. Thanks for any help anyone can give me. Quote Link to comment https://forums.phpfreaks.com/topic/257439-return-rows/ Share on other sites More sharing options...
trq Posted February 21, 2012 Share Posted February 21, 2012 See mysql_num_rows. Quote Link to comment https://forums.phpfreaks.com/topic/257439-return-rows/#findComment-1319457 Share on other sites More sharing options...
steviemac Posted February 21, 2012 Author Share Posted February 21, 2012 Thanks for the help. I did this: <?PHP include 'db_con.php'; $month = date("n"); $day = date("j"); $sql = mysql_query("SELECT * FROM fallen WHERE month = $month AND day = $day ")or die(mysql_error()); $numrows = mysql_num_rows($sql); while ($row = mysql_fetch_array($sql)) if ($numrows == 1) { echo "<div align=\"center\"><h3>On This Date</h3> <p><img src=\"fallen/images/$row[image].jpg\" alt=\"$row[fname] $row[lname]\" /><br /> $row[rank] $row[fname] $row[lname]</p></div>"; } elseif ($numrows > 1) { echo"<p>$row[fname] $row[lname]</p>"; } else { echo "<div align=\"center\"><h3>On This Date<br /><br /><img src=\"images/salute.jpg\" width=\"230\" height=\"105\" alt=\"salute\"/> "; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/257439-return-rows/#findComment-1319473 Share on other sites More sharing options...
steviemac Posted February 21, 2012 Author Share Posted February 21, 2012 It looks like I spoke to soon. If I have 0 rows it will not return properly. I if have one or more rows then it works fine. I have tried this <?PHP include 'db_con.php'; $month = date("n"); $day = date("j"); $sql = mysql_query("SELECT * FROM fallen WHERE month = $month AND day = $day ")or die(mysql_error()); $numrows = mysql_num_rows($sql); while ($row = mysql_fetch_array($sql)) if ($numrows == 1) { echo "<div align=\"center\"><h3>On This Date</h3> <p><img src=\"fallen/images/$row[image].jpg\" alt=\"$row[fname] $row[lname]\" /><br /> $row[rank] $row[fname] $row[lname]</p></div>"; } elseif ($numrows > 1) { echo"<p>$row[fname] $row[lname]</p>"; } else { echo "<div align=\"center\"><h3>On This Date<br /><br /><img src=\"images/salute.jpg\" width=\"230\" height=\"105\" alt=\"salute\"/> "; } ?> I have also tried this <?PHP include 'db_con.php'; $month = date("n"); $day = date("j"); $sql = mysql_query("SELECT * FROM table WHERE month = $month AND day = $day ")or die(mysql_error()); $numrows = mysql_num_rows($sql); while ($row = mysql_fetch_array($sql)) if ($numrows == 0)//I HAVE ALSO USE < 1// { echo "<div align=\"center\"><h3>On This Date<br /><br /><img src=\"images/salute.jpg\" width=\"230\" height=\"105\" alt=\"salute\"/> <br /><br/>T "; } elseif ($numrows == 1) { echo "<div align=\"center\"><h3>On This Date</h3> <p><img src=\"fallen/images/$row[image].jpg\" alt=\"$row[fname] $row[lname]\" /><br /> $row[rank] $row[fname] $row[lname]</p></div>"; } elseif ($numrows > 1) { echo"<p>$row[rank] $row[fname] $row[lname].</p>"; } ?> and both do not work when rows = 0. I appreciate any help anyone can offer. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/257439-return-rows/#findComment-1319730 Share on other sites More sharing options...
trq Posted February 21, 2012 Share Posted February 21, 2012 You have your check within your while loop. What do you think the while loop will do when there are 0 rows? Quote Link to comment https://forums.phpfreaks.com/topic/257439-return-rows/#findComment-1319747 Share on other sites More sharing options...
litebearer Posted February 21, 2012 Share Posted February 21, 2012 Hmmm... <?PHP include 'db_con.php'; $month = date("n"); $day = date("j"); /* puting your query in a variable will allow you to echo the query for problem solving */ $query = "SELECT * FROM table WHERE month = '$month' AND day = '$day' "; $sql = mysql_query($query)or die(mysql_error()); $numrows = mysql_num_rows($sql); switch ($numrows){ case 0: echo "<div align=\"center\"><h3>On This Date<br /><br /><img src=\"images/salute.jpg\" width=\"230\" height=\"105\" alt=\"salute\"/><br /><br/>T "; break; case 1: $row = mysql_fetch_array($sql) echo "<div align=\"center\"><h3>On This Date</h3><p><img src=\"fallen/images/$row[image].jpg\" alt=\"$row[fname] $row[lname]\" /><br />$row[rank] $row[fname] $row[lname]</p></div>"; break; default: while ($row = mysql_fetch_array($sql)) { echo"<p>$row[rank] $row[fname] $row[lname].</p>"; } break; } Quote Link to comment https://forums.phpfreaks.com/topic/257439-return-rows/#findComment-1319762 Share on other sites More sharing options...
steviemac Posted February 21, 2012 Author Share Posted February 21, 2012 I thought if the rows were = 0 it would display the html code I have tried it with out the while loop but when I get $numrows > 1 it will only display one row. Do I end the while loop and the repeat the other code $numrows = 1? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/257439-return-rows/#findComment-1319767 Share on other sites More sharing options...
steviemac Posted February 21, 2012 Author Share Posted February 21, 2012 Thanks litebearer! That was the solution. Quote Link to comment https://forums.phpfreaks.com/topic/257439-return-rows/#findComment-1319769 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.