Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7699053
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:23:18+00:00 2026-05-31T22:23:18+00:00

I know this question has been asked before numorous times but they seem to

  • 0

I know this question has been asked before numorous times but they seem to have a desktop application background and not web application. I am currently building a web application using PHP. I’m going to use a controller and model file to demonstrate my question.

Controller file

This file calls upon the database to query and fetch data

<?php public function index() {
    $this->database->query("SELECT user_name, FROM test WHERE user_name = :user_name");
    $this->database->execute_query("jim");
    $this->view->data = $this->database->fetch_query();
?>

The database file

<?php

class DB {

    private $datasourcename;
    private $user;
    private $password;
    private $connection;
    private $prepare;
    private $query;

    function __construct($dsn, $user, $password) {

        $this->datasourcename = $dsn;
        $this->user = $user;
        $this->password = $password;    

        $this->connection = new PDO($this->datasourcename, $this->user, $this->password);
    }

    public function query($query) {

        $this->query = $query;
        try {
            if (empty($query)) {

                throw new Exception("The query is empty");
                return false;
            }
        } catch (Exception $e) {
            echo 'Caught exception: ', $e->getMessage(), "<br/>";
        }
        if (strstr($query, ":") == FALSE) {
            return $this->connection->query($query);
        } else {

            $this->prepare = $this->connection->prepare($query);
        }
    }

    public function execute_query($valarg = array()) {

        try {
            if (empty($valarg)) {

                throw new Exception("There are no values in the execute query function");
                return false;
            }
            if (is_array($valarg) == false) {

                throw new Exception("The values inserted are not in an array");
                return false;
            } else {

                $query = $this->query;
                $paramkeys = array();
                $paramArr = array();

                if (strstr($query, ":")) {    

                    preg_match_all("/:(\w+)/", $query, $paramkeys);

                    $paramArr = array_combine($paramkeys[0], $valarg);

                    $this->prepare->execute($paramArr);
                }
            }
        } catch (Exception $e) {
            echo 'Caught exception: ', $e->getMessage(), "<br/>";
        }
    }

    public function fetch_query() {

        try {
            if($this->execute_query() == false)    {

                 throw new Exception("Sorry you need to fix this first");
            }

             if($this->query() == false)    {

                 throw new Exception("Sorry you need to fix this first");
            }
            else    {

                 $result = $this->prepare->fetch(PDO::FETCH_ASSOC);

                return $result;
            }
        }
        catch (Exception $e) {
            echo 'Caught exception: ', $e->getMessage(), "<br/>";
        }
    }
}

?>

I have used exception handling in my database class as a way for a developer to use the functions provided by that class correctly in order for the application to work, am I right im thinking that this is the best practise to use them? Is there a clear cut best practise when and how to use them? I have read similar questions on this site but the background was desktop application design and not web application.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:23:20+00:00Added an answer on May 31, 2026 at 10:23 pm

    Web programming is no different than desktop programming in this regard.

    Use exceptions for exceptional events. Use return values for anything expected.

    I think seeing an empty query result isn’t something that will have an exception thrown, generally. I’d just return no data. If the database query failed somehow, that would be a reason to throw an exception. Also, your exception messages should probably be more descriptive, rather than “you need to fix this”.

    Finally, in PHP 5.3, you can set an inner exception by passing it as the third parameter in the Exception constructor.

    http://www.php.net/manual/en/class.exception.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question has been asked before but I seem to have a
I know this question has been asked many times before but I can't find
I know this question has been asked so many times before but I couldn't
I know this question has been asked many times before but I tried out
I know this question has been asked before, but I have tried the given
I know this question has been asked countless times before, but the other threads
I know that maybe this question has been asked before, but I can't seem
I know this question has been asked before but I have a design question
I know this question has been asked before, but I ran into a problem.
I know this specific question has been asked before , but I am not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.