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 7769705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:13:27+00:00 2026-06-01T16:13:27+00:00

Sometimes depending on which user type if viewing my page, I need to add

  • 0

Sometimes depending on which user type if viewing my page, I need to add in a JOIN, or even just limit the results. Is there a cleaner way of going about it? Should I have separate statements for each type of request instead? What is more “proper”?

Here is what my code ends up looking like:

// Prepare statement
$stmt = $this->db->prepare('
    SELECT *
    FROM    Documents
    LEFT JOIN Notes ON ID = D_ID
    '.($user_id ? "INNER JOIN Users ON UID = ID AND UID = :userid" : '')."
    ". ($limit ? 'LIMIT :offset, :limit' : '')
);

// Bind optional paramaters
if ($user_id) $stmt->bindParam(':userid', $user_id, DB::PARAM_INT);

if ($limit)
{
    $stmt->bindParam(':offset', $limit[0], DB::PARAM_INT);
    $stmt->bindParam(':limit', $limit[1], DB::PARAM_INT);
}
  • 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-06-01T16:13:28+00:00Added an answer on June 1, 2026 at 4:13 pm

    I’d create separate (protected) functions, those return a prepared statement that only needs to be executed.

    /**
    * @returns PDOStatement
    */
    protected function prepareStatementForCase1(PDO $dbObject,Object $dataToBind){...}
    /**
    * @returns PDOStatement
    */
    protected function prepareStatementForCase2(PDO $dbObject,Object $dataToBind){...}
    

    Then, I would decide outside, which one has to be called.
    You can rebuild, maintain and read the code more easily.

    Example:

    class Document{
      protected $dbObject;
    
      public function __construct(PDO $dbObject){
        $this->dbObject=$dbObject;
      }
      public function doQuery($paramOne,$paramTwo,...){
        $logicalFormulaOne=...; // logical expression here with parameters
        $logicalFormulaTwo=...; // logical expression here with parameters
        if($logicalForumlaOne){
          $dbStatement=$this->prepareStatementForCase1($dataToBind);
        }else if($logicalFormuleTwo){
          $dbStatement=$this->prepareStatementForCase2($dataToBind);
        }
        $dbResult=$dbStatement->execute();
      }
      protected function prepareStatementForCase1(Object $dataToBind){
        $dbStatement=$this->dbObject->prepare("query string");
        $dbStatement->bindParam(...);
        return $dbStatement;
      }
    }
    

    But I would not advice this, when your PDOResult object represents different type of database tuples, or when you return more rows in one of the cases.

    What I usually do is that I create a class which represents (in your example) a Document. Only one. I can insert, delete, select, modify by its fields, and handle one item. When I need to (for example) fetch more of them, I create a new class, e.g. DocumentList, which handles a collection of documents. This class would give me an array of Document objects when it fetches more of them.

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

Sidebar

Related Questions

I program in C++. Sometimes there are 1000 ways to do something, and depending
I have a Java swing application which loads a web page. Sometimes, the web
Sometimes when I'm editing page or control the .designer files stop being updated with
Sometimes I need to quickly extract some arbitrary data from XML files to put
Sometimes, in PL SQL you want to add a parameter to a Package, Function
Sometimes I get Oracle connection problems because I can't figure out which tnsnames.ora file
A user can perform an action, which may trigger dependent actions (which themselves may
I'm currently re-writing a class which handles xml files. Depending on the xml file
Which do you guys prefer? I've been looking into both and there definitely seems
I need a way to layout controls in a page. Let me ask you

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.