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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T17:33:34+00:00 2026-06-18T17:33:34+00:00

Since today I passed the variable of my non-static database to the functions of

  • 0

Since today I passed the variable of my non-static database to the functions of my php project, that needed database access.
I read all about multiple database connections, so I changed my database connection to a static function, so I have in all my functions access to my database connection without passing it as a function´s parameter. So, it is possible to access my database connection (even different databases) by using e.g.: myDatabaseConnection::myFirstdatabase->prepare and so on…

The thing is: What is best practice? Is it good to do that this way. Some of my functions (for example to read some data out of the database) depend on an database connection, but if I remove the database connection variable from the parameters of the function it seems to me that something (a kind of dependency) is missing.

So what is the best practice for that? Do you pass the database connection as a variable to your functions that need database access?

Thank you very much.

I understand the thing with the connection.
But what about the other functions in my project; like e.g. a function to read all member names out of a database?
Do you pass a database connection variable to that function or do you access the connection to the database inside of that function using my “global” static database conncection. I know, that it is not global, but my static database connection can be accessed from everywhere…

  • 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-18T17:33:35+00:00Added an answer on June 18, 2026 at 5:33 pm
    <?php
    require_once('CLASSdatabase.php');
    
    class AUTHENTICATE{
        private $user_database_object;
        private $current;
        private $email_id;
        private $password;
        private $error;
    
        function __construct($email_id,$password){
            $this->user_database_object=new MYSQL_DATABASE("user");
            $this->email_id=$email_id;
            $this->password=$password;
        }
    
        private function verify_password(){
            $sql="select password from `users` where email_id='";
            $sql.=$this->email_id."';";
            $this->user_database_object->open_connection();
            $row=$this->user_database_object->fetch_array($this->user_database_object->query($sql));
            // code ...
            $this->user_database_object->close_connection();
        }
    }
    // code ..
    ?>
    

    CLASSdatabase.php

    <?php
    require_once("config.php");
    
        class MYSQL_DATABASE{
            private $dbname;
            private $connection;
            public $last_query;
    
            function __construct($dbname){
            $this->dbname=$dbname;
            }
    
            public function open_connection(){
            }
            public function close_connection(){
            }
            public function query($sql){
            }
            public function fetch_array($result_set){
            }
            // code ...
        }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have always been taught that non-primitive types should be passed by const reference
Since today I am facing a tricky issue with Google Chrome that I've just
I wrote a text mode batch C++ program that since today is giving me
Since today I've started getting errors when I try to delete IndexedDB database in
It's been some time since I created a new project in Xcode. Today I
I'm looking for a way to count the months that has passed, since a
since today im facing a strange problem. When i start my app in my
This is a simplified version of my question earlier today since I didn't get
Since REST is stateless, each request that comes in has no knowledge of the
Today I found this query in our code that pulls up a list of

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.