PHP what to do when the records do not exist in the database


I'm querying the database for a single record that matches the id passed in a url segment

Suppose post 24 doesn't exist. What is a safe and seo compliant way to deal with database requests that aren't found?

Below is my current solution using a Codeigniter model:

    function read_post($post_id) //argument is the value of the url segment
        $user_id = $this->tank_auth->get_user_id();

        $this->db->where('user_id', $user_id);
        $this->db->where('post_id', $post_id);
        $query = $this->db->get('posts');

        //check if record exists
        if ($query->num_rows() > 0) //row is returned
            return $query->row(); //passed to controller and view
            show_404(); //generate 404 message
            return FALSE; //------------------------or exit()?

Please share your own best method/opinions.

Show a 404 page (with the 404 HTTP status code). That's the correct way of doing it. Otherwise you will just pretend you have infinitely many pages without content.

Describe shortly on the 404 page that the content isn't there anymore. Then you may add links to other popular content on your web page. For the sake of the user it's encouraged to try to lead them forward from the 404 page to interesting content, but do this with links so the user can read the 404 message and choose what to do next.

If you have meaningful URLs with words in you may also extract the words and do some kind of search in your database. List the search results on the 404 page as well, under some heading like "You may be interested in...". If you are selling a product or service, don't hesitate to make the 404 page into a nice selling one!

I have a strong belief in that the best SEO is the one which is best for your visitors. It's all about keeping them on your web site, and not scaring them away. Make your web site as user-friendly as possible and make it easy to find all your interesting content. My answer above stems directly from that belief. Do The Right Thing according to the standards (the HTTP standard in this case) and at the same time try to catch the user with good content and services. This is most certainly also what's best for your company at large regarding sales.