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

  • Home
  • SEARCH
  • 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 3757686
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T10:02:43+00:00 2026-05-19T10:02:43+00:00

I’m sure what i’m currently doing is not optimal , but I would really

  • 0

I’m sure what i’m currently doing is not optimal, but I would really like to hear some opinions regarding how you should handle all db queries on a web application including the connection string.

At the moment, I have a classes directory that has a file for each class and I have another directory called db that includes a conn.php which has the connection string and the other files on the db directory are like in the classes directory, one for each class but to handle the mysql queries.

So basically I have all db queries per class in one file and whenever I need to query something from the class file, I call the function on the db file

I include each db file in the corresponding db file, for example in the user.class.php file you will find include('db/user.db.php').

Also, I include the conn.php file to each db file.

user.class.php:

include('db/user.db.php');
class User {
    public $fname;
    public $userid;

    function __construct($userid) {
        $this->user_id = $userid;
        $this->fname = DB_GetFirstName($userid);
        }
}

user.db.php:

include('conn.php');
function DB_GetFirstName($userid) {
    $result = mysql_fetch_array(mysql_query("SELECT USR_FName FROM users WHERE USR_ID = '$userid'"));
    return $result[0];
}

conn.php:

$conn = mysql_connect("localhost", "user", "pass");
mysql_select_db("dbname", $conn);

How do you handle it?

  • 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-19T10:02:44+00:00Added an answer on May 19, 2026 at 10:02 am

    You are doing the right thing already by separating your Domain Layer from the Database Access Layer. But the way you put it together could be improved. Have a look at the Table Data Gateway (TDG) pattern.

    In the Table Data Gateway pattern one class encapsulates all the access to a specific table. This is kind of like your User.Db.php with the difference that the TDG is an actual class. Instead of a bunch of functions you group any related db access into that particular class. This has the immediate benefit that you can pass the instance to any class that needs it instead of hardcoding function calls into them.

    Whenever you need to work with a specific table you use the TDG to fetch/modify rows from it. You can then either work with the returned recordsets. Or use a DataMapper to map the data from the recordset onto your Domain classes, e.g. your User class. For simple DataMappers crafting your own is fine. Once it gets more complex, you’ll better of using an existing ORM.

    An alternative to the TDG would be the Row Data Gateway pattern.

    There is a good introduction to TDGs (with a Zend Framework example) at

    • http://css.dzone.com/books/practical-php-patterns-table

    and for Row Data Gateway

    • http://css.dzone.com/books/practical-php-patterns/practical-php-patterns-row
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.