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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:49:02+00:00 2026-05-11T09:49:02+00:00

Working on a particular application, I keep writing very similar queries, again and again.

  • 0

Working on a particular application, I keep writing very similar queries, again and again. They’re not exactly the same, but are of very similar form, and embedded in almost identical chunks of code, e.g.,

$Mysqli = new mysqli; if ($Stmt = $Mysqli->prepare('SELECT foo                                FROM tblFoo                                WHERE something = ?')) {     $Stmt->bind_param('s', $this->_something);     $Stmt->execute();     if (0 != $Stmt->errno)         throw new Exception('blah, blah, blah');     $Stmt->bind_result($foo);     while ($Stmt->fetch()){         $this->_foos[] = new Foo($foo);     }     $Stmt->close();     } else {         throw new Exception('blah, blah, blah'););     } } 

and later, somewhere else …

$Mysqli = new mysqli; if ($Stmt = $Mysqli->prepare('SELECT bar, baz                                FROM tblBar                                WHERE somethingElse = ?')) {     $Stmt->bind_param('s', $this->_somethingElse);     $Stmt->execute();     if (0 != $Stmt->errno)         throw new Exception('blah, blah, blah');     $Stmt->bind_result($bar, $baz);     while ($Stmt->fetch()){         // do something else with $bar and $baz     }     $Stmt->close();     } else {         throw new Exception('blah, blah, blah'););     } } 

… and then another, and elsewhere another … etc.

Is this a real violation of DRY? It doesn’t seem to make sense to write a class for performing this kind of query (with constructor params or setters for table, column, bound variables, etc.) and then reusing it throughout my app. But at the same time, I can’t shake this nagging feeling that I’m repeating myself.

Maybe it’s just that there are only so many ways to write a simple query and that a certain amount of repetition like this is to be expected.

Thoughts?

  • 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. 2026-05-11T09:49:02+00:00Added an answer on May 11, 2026 at 9:49 am

    A lot of people do exactly this. Create a class representing each table, that all inherit from the same class. The base class can handle loading and saving the data. So if you want to load the data, you just call the load method. And you can set and access the field values by the properties of the objects.

    There’s also libraries like hibernate that handle a lot of the dirty work for you.

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

Sidebar

Related Questions

I'm still working on a particular RDF file, but seems that something is not
I've been working on this application but have come across a problem that I
I am working on an application which currently has a plugin to a particular
I'm working on the long-poll application which uses node.js as a server. The particular
I'm working on particular application, a spring+hibernate setup. I have one front end in
I'm currently working on applying genetic algorithms to a particular application, and the issue
I am working on the UI for a particular java application. I need to
I need to understand the working of this particular program, It seems to be
If you are in a corporate setting with many people working on a particular
I am working with SiteFinity CMS. My particular layout is 3-column with 100% width

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.