Need quick help with "or die()"

Hey guys.

So I have a news feature on my website and is displayed by id. I’m trying to set it up so if users come across an article that has been deleted or never created, then they get a message saying that the record doesn’t exist.

I tried setting it up as follows:

<?php echo 
nl2br( ( $row_news['news'] ) ) or die("The id number specified was either deleted or never created in our database."); 

It works when I enter a number that hasn’t been created yet, however when I go to a number that has already been created, I just get the number “1” instead of the content that I want. What am I doing wrong?

Here’s my old code:

<?php echo 
nl2br( ( $row_news['news'] ) ); 

The n12br() is just a way for me to insert a break when writing an article. The data is being retrieved from a phpMyAdmin database.

I’m not so fluent in PHP as you can tell, so if this is wrong, then thanks for telling me.

I just want to return a message for people that are randomly typing numbers in my URL to bring up different articles. I don’t want them to just be brought up with a blank page.

You probably want to use an if statement instead of dieing. Dieing really should only be used as a last resort.

if (isset($row_news['news'])) {
	echo nl2br($row_news['news']);
else {
	echo "The id number specified was either deleted or never created in our database.";

nl2br() doesn’t return true or false.
How can you return information that doesn’t exist?
Judging by your naming, I would assume that your returning from a database. So how and why would you return deleted values, etc?

However, depending on how you want to validate your data for output try something like this:

if( /* something to validate */ ){
      // Article exists - use the nl2br() function here.
} else {
      // Article does not exist

The validation could use something like strlen($var) > 0, $id != X, etc

To be honest, if the data has been deleted or hasn’t been created then you shouldn’t be returning it.

Hope that helps in some way.


Hope that helps.


Beat me to it hehe

This worked! Thank you very much.

I started reading a little bit on die() and it doesn’t seem to be very popular.

Thanks again.

If the records don’t exist, then you cant return them.

So if your querying your database do something like the following:

$query = mysql_query( /* query */ );

// Check to see records have been returned. mysql_num_rows() returns integer value of 0+
if( mysql_num_rows( $query ) > 0 ){
   // record(s) have been returned, do something with them
} else {
   // Nothing has been returned so tell the visitor that you could not find an item with the ID.

When you get the chance, have a look at these links
and have a read through the PHP & MySQL tutorials on tizag. Whilst the Tizag tutorials are quite basic, they are a great starting point.

Hope that helps.