Jump to content

displaying a msg if nothing in database


Ameslee

Recommended Posts

<?php

  // connect to db
  $sql = "SELECT data FROM foo";
  if ($result = mysql_query($sql)) {
    if (mysql_num_rows($result)) {
      // display data
    } else {
      //no records found.
      echo "come back soon";
    }
  } else {
    echo "query failed ".mysql_error();
  }

?>

thanks, could that be put into my existing code?

heres my exisiting code with the new code added in:

<?php
			$hostname = localhost;
			$username = ;
			$password = ;

			$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
			$connection = mysql_select_db("greenac_VORNExpo", $conn);


			$query = "SELECT * FROM events WHERE Date >=now() ORDER BY Date ASC";
			$mysql_result=mysql_query($query,$conn);
			$row=mysql_fetch_array($mysql_result);

			echo "<table width=70% border=0 cellpadding=0 cellspacing=0>";
			$i=1;
			while (($row!="") && ($i<=7))
			{
			$date=$row[2];		
			$day=substr($date,8,2);	
			$month=substr($date,5,2);	
			$year=substr($date,0,4);
			$date2=$day." - ".$month." - ".$year;
			$event = $row[1];


			echo("<tr><td width=30%>$event</td><td width=15%>$date2</td></tr>
                    ");
			$row=mysql_fetch_array($mysql_result);
			$i++;
			} else {
      //no records found.
      echo "come back soon";
    }
  } else {
    echo "query failed ".mysql_error();
  }


			?>

Try this. Copy and paste should work.

 

<?php
$hostname = localhost;
$username = ;
$password = ;

$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
$connection = mysql_select_db("greenac_VORNExpo", $conn);

$query = "SELECT * FROM events WHERE Date >=now() ORDER BY Date ASC";
$mysql_result=mysql_query($query,$conn);

$login_check = mysql_num_rows($query);

if($login_check > 0){

echo "<table width=70% border=0 cellpadding=0 cellspacing=0>";
$i=1;
while (($row = mysql_fetch_array($mysql_result)) && ($i<=7))
{
$date=$row[2];		
$day=substr($date,8,2);	
$month=substr($date,5,2);	
$year=substr($date,0,4);
$date2=$day." - ".$month." - ".$year;
$event = $row[1];

echo("<tr><td width=30%>$event</td><td width=15%>$date2</td></tr>");

$i++;
}

} else {

echo "No data!";

} else {

echo "query failed ".mysql_error();

}


?>

i have changed the code and im getting a different error.  here is the code:

 

<?php
			$hostname = localhost;
			$username = greenac_Admin;
			$password = greenac;

			$conn = mysql_connect($hostname, $username, $password) or die(mysql_error());
			$connection = mysql_select_db("greenac_VORNExpo", $conn);


			$query = "SELECT * FROM events WHERE Date >=now() ORDER BY Date ASC";
			$mysql_result=mysql_query($query,$conn);
			echo "<table width=70% border=0 cellpadding=0 cellspacing=0>";
			$nothingInDatabase= true;
			$i = 1
			while ($row = mysql_fetch_array($mysql_result) && $i<=7) {
   				if ($nothingInDatabase) $nothingInDatabase = false;
   					$date=$row[2];		
   					$day=substr($date,8,2);	
   					$month=substr($date,5,2);	
  					$year=substr($date,0,4);
   					$date2=$day." - ".$month." - ".$year;
   					$event = $row[1];

   				echo("<tr><td width=30%>$event</td><td width=15%>$date2</td></tr>");
   					$i++;
			}
			if ($nothingInDatabase == true) {
				echo "<tr><td>Please check back later for entries</td></tr>";
				}
			echo "</table>";


			?>

 

the new error is: Parse error: syntax error, unexpected T_WHILE in /home/greenac/public_html/events.php on line 201

 

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.