stublackett Posted September 25, 2008 Share Posted September 25, 2008 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>"; ?> Link to comment https://forums.phpfreaks.com/topic/125797-solved-show-a-message-if-an-sql-row-is-empty/ Share on other sites More sharing options...
F1Fan Posted September 25, 2008 Share Posted September 25, 2008 Try using mysql_num_rows (http://us.php.net/manual/en/function.mysql-num-rows.php) in an if statement. Link to comment https://forums.phpfreaks.com/topic/125797-solved-show-a-message-if-an-sql-row-is-empty/#findComment-650481 Share on other sites More sharing options...
rcouser Posted September 25, 2008 Share Posted September 25, 2008 <?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 Link to comment https://forums.phpfreaks.com/topic/125797-solved-show-a-message-if-an-sql-row-is-empty/#findComment-650486 Share on other sites More sharing options...
adam291086 Posted September 25, 2008 Share Posted September 25, 2008 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>"; } ?> Link to comment https://forums.phpfreaks.com/topic/125797-solved-show-a-message-if-an-sql-row-is-empty/#findComment-650489 Share on other sites More sharing options...
stublackett Posted September 25, 2008 Author Share Posted September 25, 2008 Both empty and ! worked........ Cheers chaps Link to comment https://forums.phpfreaks.com/topic/125797-solved-show-a-message-if-an-sql-row-is-empty/#findComment-650490 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.