Jump to content

[SOLVED] Show a message if an SQL Row is empty?


stublackett

Recommended Posts

Hi,

 

I've got a page setup at the moment that is pulling data from a Database, But at present the Dataabse is empty, So I'm getting undefined variables everywhere... There maybe a way to correct that?

 

I'm thinking for the time being that if the SQL Select statement returns no values to show the message "There are currently no purchases"

 

How do I achieve this, With My code which is as follows :

<?php

//Make DB Connection to retrieve User Info
mysql_connect($hostname, $username, $password)or die("cannot connect");
mysql_select_db($database)or die("cannot select DB");
$query= "SELECT * FROM bdExperiences WHERE experiencename = 'Keeper For A Day'";
$result=mysql_query($query) or die (mysql_error());

//Select ALL Data
while ($row= mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['sendertitle'];
$forename = $row['senderforename'];
$surname = $row['sendersurname'];
$address = $row['senderaddress'];
$address2 = $row['senderaddress1'];
$town = $row['sendertown'];
$county = $row['sendercounty'];
$telephone = $row['sendertelephone'];
$email = $row['senderemail'];
$numberofpeople = $row['numberofpeople'];

$reducedrate = $row['numberforreducedrate'];

$total = $row['total'];
}

echo "<div id='recipient'>";
echo "<h2>";
echo "Purchase Number $id </h2>";
echo "<h2>Buyers Details</h2>";
echo "<ul>";
echo "<li>"; echo "Name : $title"; echo " "; echo $forename; echo " "; echo $surname; echo "</li>";
echo "<li>"; echo "Address : "; echo $address; echo " "; echo $address2; echo ","; ; echo $town; echo ","; echo " "; echo $county; echo "</li>";
echo "<li>"; echo "Telephone :"; echo " "; echo $telephone; echo "</li>";
echo "<li>"; echo "E Mail :"; echo " "; echo $email; echo "</li>";
echo "<br><br>";
echo "</ul>";
echo "</div>";

echo "<p>Number of recipients :"; echo " "; echo"<b><u>"; echo $numberofpeople; echo "</b></u>"; 
echo "</p>";
echo "<p>Number of people entering park at reduced rate : "; echo"<b><u>"; echo $reducedrate; echo "</b></u>";

echo "<div align='center'>";
echo "<h2>Total : £<b><u>$total</h2></b></u>";
echo "</div>";
?>

<?php

//Make DB Connection to retrieve User Info
mysql_connect($hostname, $username, $password)or die("cannot connect");
mysql_select_db($database)or die("cannot select DB");
$query= "SELECT * FROM bdExperiences WHERE experiencename = 'Keeper For A Day'";
$result=mysql_query($query) or die (mysql_error());

//Select ALL Data
while ($row= mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['sendertitle'];
$forename = $row['senderforename'];
$surname = $row['sendersurname'];
$address = $row['senderaddress'];
$address2 = $row['senderaddress1'];
$town = $row['sendertown'];
$county = $row['sendercounty'];
$telephone = $row['sendertelephone'];
$email = $row['senderemail'];
$numberofpeople = $row['numberofpeople'];

$reducedrate = $row['numberforreducedrate'];

$total = $row['total'];
}


if (!$id) {
echo 'There are currently no purchases';
}
else {
echo "<div id='recipient'>";
echo "<h2>";
echo "Purchase Number $id </h2>";
echo "<h2>Buyers Details</h2>";
echo "<ul>";
echo "<li>"; echo "Name : $title"; echo " "; echo $forename; echo " "; echo $surname; echo "</li>";
echo "<li>"; echo "Address : "; echo $address; echo " "; echo $address2; echo ","; ; echo $town; echo ","; echo " "; echo $county; echo "</li>";
echo "<li>"; echo "Telephone :"; echo " "; echo $telephone; echo "</li>";
echo "<li>"; echo "E Mail :"; echo " "; echo $email; echo "</li>";
echo "<br><br>";
echo "</ul>";
echo "</div>";

echo "<p>Number of recipients :"; echo " "; echo"<b><u>"; echo $numberofpeople; echo "</b></u>"; 
echo "</p>";
echo "<p>Number of people entering park at reduced rate : "; echo"<b><u>"; echo $reducedrate; echo "</b></u>";

echo "<div align='center'>";
echo "<h2>Total : £<b><u>$total</h2></b></u>";
echo "</div>";
}
?>

As you can see, the if statement checks to see if there is any id in the database, if no display your message

try

 

<?php

//Make DB Connection to retrieve User Info
mysql_connect($hostname, $username, $password)or die("cannot connect");
mysql_select_db($database)or die("cannot select DB");
$query= "SELECT * FROM bdExperiences WHERE experiencename = 'Keeper For A Day'";
$result=mysql_query($query) or die (mysql_error());

//Select ALL Data
while ($row= mysql_fetch_array($result)) {
$id = $row['id'];
$title = $row['sendertitle'];
$forename = $row['senderforename'];
$surname = $row['sendersurname'];
$address = $row['senderaddress'];
$address2 = $row['senderaddress1'];
$town = $row['sendertown'];
$county = $row['sendercounty'];
$telephone = $row['sendertelephone'];
$email = $row['senderemail'];
$numberofpeople = $row['numberofpeople'];

$reducedrate = $row['numberforreducedrate'];

$total = $row['total'];
}


if (empty($id)) {
echo 'There are currently no purchases';
}
else {
echo "<div id='recipient'>";
echo "<h2>";
echo "Purchase Number $id </h2>";
echo "<h2>Buyers Details</h2>";
echo "<ul>";
echo "<li>"; echo "Name : $title"; echo " "; echo $forename; echo " "; echo $surname; echo "</li>";
echo "<li>"; echo "Address : "; echo $address; echo " "; echo $address2; echo ","; ; echo $town; echo ","; echo " "; echo $county; echo "</li>";
echo "<li>"; echo "Telephone :"; echo " "; echo $telephone; echo "</li>";
echo "<li>"; echo "E Mail :"; echo " "; echo $email; echo "</li>";
echo "<br><br>";
echo "</ul>";
echo "</div>";

echo "<p>Number of recipients :"; echo " "; echo"<b><u>"; echo $numberofpeople; echo "</b></u>"; 
echo "</p>";
echo "<p>Number of people entering park at reduced rate : "; echo"<b><u>"; echo $reducedrate; echo "</b></u>";

echo "<div align='center'>";
echo "<h2>Total : £<b><u>$total</h2></b></u>";
echo "</div>";
}
?>

 

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.