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
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.