Jump to content

return rows


steviemac

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/257439-return-rows/
Share on other sites

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\"/>
";
}   


?>

Link to comment
https://forums.phpfreaks.com/topic/257439-return-rows/#findComment-1319473
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/257439-return-rows/#findComment-1319730
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/257439-return-rows/#findComment-1319762
Share on other sites

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.