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

The Archive Base Latest Questions

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

$dsn=mysql://$db_username:$db_password@$db_hostname/$db_database; global $mdb2; $mdb2=MDB2::connect($dsn); if (PEAR::isError($mdb2)) { die($mdb2->getMessage()); } I do this to connect

  • 0
$dsn="mysql://$db_username:$db_password@$db_hostname/$db_database";
global $mdb2;
$mdb2=MDB2::connect($dsn);
if (PEAR::isError($mdb2))
{
    die($mdb2->getMessage());
}

I do this to connect to my DB, I put this in a separate php file called Connect.php and require it on all my pages.

However, when I have to query inside a function, I will have to pass $mdb2 to the function as an argument? Is this the right way to do it.

Further, I am writing a class which will query my DB. And I have no idea what to do (I don’t wanna pass it as an argument)

Do I have to re-establish the connect everytime (ie. write a function for connection)

Can’t you make the connection persistent and global?

  • 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-23T03:19:03+00:00Added an answer on May 23, 2026 at 3:19 am

    You can require your file Connect.php on all of your pages, and every function that needs to use the connection can refer to the global variable $mdb2.

    For example:

    # In file Connect.php
    
    <?php
    $dsn="mysql://$db_username:$db_password@$db_hostname/$db_database";
    $mdb2=MDB2::connect($dsn);
    if (PEAR::isError($mdb2))
    {
        die($mdb2->getMessage());
    }
    
    
    #In any other file
    
    <?php
    require_once "Connect.php";
    getUser($id) {
        global $mdb2;
        $mdb2->query("SELECT ....");
    }
    

    Other solution is using a Singleton Class to access the database, so that there is a function that always returns the reference to your $mdb2 variable.

    Surely, the discussion Global or Singleton for database connection? is something worth reading.

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

Sidebar

Related Questions

try to set host in dsn of pdo like this: <?php /* Connect to
I think about switching to DB access library that uses DSN connection - connect(mysql://login:password@database?options).
I've been trying to connect Excel to online mysql database. Im using 5.1 DSN
I am looking for a way how to convert a PEAR:DB / MDB2-style DSN
This works with a mySQL backend In the form... <cfselect name=to size=1 bind=cfc:cfcs.messages.getOrganisations() bindonload=yes
I'm moving from an MS Access backend to mySQL. This used to work but
I am using MySQL and PHP 5.3 and tried this code. $dbhost = 'localhost';
Is there anyway to create a System DSN enter for MYSQL database (windows OS),
Is there a way to connect MySQL to VB6 at design time ? I
Here is my config/databases.yml: all: galapagos: class: sfDoctrineDatabase param: dsn: mysql:host=localhost;dbname=galapagos username: galapagos password:

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.