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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:49:13+00:00 2026-05-24T17:49:13+00:00

Let me start by explaining. I have a few global database connections and have

  • 0

Let me start by explaining.
I have a few global database connections and have a few simple functions that use each one and perform queries and such.

Because i want to use the connections more then once, and to save me defining them each time in each function i have created them as globals at the top of the document.
However i am just wondering that instead of having to write

global $mysql_db1, $mysql_db2, $mysql_db3, $mysql_db4, $mysql_db5;

Is there anyway to make this happen without me having to copy and paste it each time?

I know its trivial but i just wanted to speed up my own development,

  • 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-24T17:49:16+00:00Added an answer on May 24, 2026 at 5:49 pm

    Globals are usually regarded as bad practice. I won’t pontificate at you about it, but check out this article: http://blog.case.edu/gps10/2006/07/22/why_global_variables_in_php_is_bad_programming_practice

    You can use the $GLOBALS superglobal to access any variable that has been defined in the global scope (docs). Thus, in your example code, simply using $GLOBALS['mysql_db1'] would be the equivalent of having the line global $mysql_db1; and then using $mysql_db1.

    I cannot stress enough (without pontificating) how bad of a plan this is. YOU might be all right with it the entire time you’re developing, but poor poor Johnny Nextguy, and may the Gods of Code save you if you include a third-party script that also uses globals… and there’s a conflict of variable names. Now you’re in for it!

    As has been suggested, you are better off encapsulating your database functionality in a class. If you use a static class, you still have all the benefits of a global variable without a polluted scope or danger of overwriting.

    Here is a sample database class, along with usage:

    // put this in a library file or some place that all scripts include
    require_once('database_class.php');
    $db = new db(array(
     'host'=>'localhost'
     'user'=>'db_user_name'
     'password'=>'db_password',
     'database_name'=>'my_database'
    ));
    
    
    // now anywhere in code you want to use it
    $array = db::getRows('SELECT id, name FROM users');
    $field = db::getField('SELECT name FROM users WHERE id=10');
    
    
    <?php
    // database_class.php
    class db {
        static protected $resource_link = null;
    
        function __construct($args=false) {
            if ($args===false || !is_array($args))
                return false;
            if (
                !isset($args['host']) ||
                !isset($args['user']) ||
                !isset($args['password']) || 
                !isset($args['database_name'])
            )
                return critical_error('Missing database configuration data.');
    
    
            self::$resource_link = @mysql_connect($args['host'],$args['user'],$args['password']);
            if (!self::$resource_link)
                 return critical_error('Error connecting to database 2001.  MySQL said:<br>'.mysql_error());
            @mysql_select_db($args['database_name'],  self::$resource_link);
            return;
        }
    
    
        // return a single-dimmension array of fields as string
        static public function getFields ($sql=false, $field=false) {
            $res = null;
            if ($sql!==false) {
                $query_obj = mysql_query($sql, self::$resource_link);
                if ($query_obj) {
                    $res = array();
                     while ($this_row = mysql_fetch_array($query_obj)) {
                        if ($field !== false && isset($this_row[$field]))
                            $res[] = $this_row[$field];
                        else
                            $res[] = $this_row[0];
                    }
                } // end :: if query object is not null
            } // end :: if $sql is not false
            return $res;
        }
    
        // return a single-dimmension array of fields as string with keyfield as key
        static public function getKeyFields ($sql=false, $key_field='id', $list_field='id') {
            $res = null;
            if ($sql!==false) {
                $query_obj = mysql_query($sql, self::$resource_link);
                if ($query_obj) {
                     while ($this_row = mysql_fetch_array($query_obj)) {
                        if (isset($this_row[$key_field]))
                            $res[$this_row[$key_field]] = $this_row[$list_field];
                    }
                } // end :: if query object is not null
            } // end :: if $sql is not false
            return $res;
        }
    
        // return a single field as string from the first row of results
        static public function getField ($sql=false) {
            $res = null;
            if ($sql!==false) {
                $query_obj = mysql_query($sql, self::$resource_link);
                if ($query_obj) {
                    $this_array = mysql_fetch_array($query_obj);
                    if (is_array($this_array))
                        return $this_array[0];
                } // end :: if query object is not null
            } // end :: if $sql is not false
            return $res;
        }
    
        // return a single row as an array
        static public function getRow ($sql=false) {  
            $res = null;
            if ($sql!==false) {
                $query_obj = mysql_query($sql, self::$resource_link);
                if ($query_obj)
                    $res = mysql_fetch_assoc($query_obj);
            } // end :: if $sql is not false
            return $res;
        }
    
        // return an array of rows as arrays of strings
        static public function getRows ($sql=false) {
            $res = null;
            if ($sql!==false) {
                $query_obj = mysql_query($sql, self::$resource_link);
                $res = array();
                if ($query_obj) {
                     while ($this_row = mysql_fetch_assoc($query_obj)) {
                        $res[] = $this_row;
                    }
                } // end :: if query object is not null
            } // end :: if $sql is not false
            return $res;
        }
    
        // return an array of rows as arrays of strings, using specified field as main array keys
        static public function getKeyRows ($sql=false, $key_field='id', $include_key_in_results=true) {
            $res = null;
            if ($sql!==false) {
                $query_obj = mysql_query($sql, self::$resource_link);
                if ($query_obj) {
                    $res = array();
                     while ($this_row = mysql_fetch_assoc($query_obj)) {
                        if (isset($this_row[$key_field])) {
                            $res[$this_row[$key_field]] = $this_row;
                            if ($include_key_in_results == false)
                                unset($res[$this_row[$key_field]][$key_field]);
                        } // end :: if checking for key field in result array
                    } // end :: while looping query obj
                } // end :: if query object is not null
            } // end :: if $sql is not false
            return $res;
        }
    
    
        // do an update query, return true if no error occurs
        static public function update ($sql=false) {
            $res = false;
            if ($sql!==false) {
                $query_obj = mysql_query($sql, self::$resource_link);
                if ($query_obj && strlen(mysql_error()) < 1)
                    $res = true;
            }
            return $res;
        }
    
        // do an insert query, return the auto increment ID (if there is one)
        static public function insert ($sql=false) {
            $res = null;
            if ($sql !== false) {
                $query_obj = mysql_query($sql, self::$resource_link);
                if ($query_obj)
                    $res = mysql_insert_id(self::$resource_link);
            }
            return $res;
        }    
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First let me start by explaining my use case: Say there is a database
let me start with that i am pretty new to c# currently i have
Let me start by example... I have an all.proj that looks similar to this:
Let me start with explaining what I mean with magic. I will use two
Let me start by explaining our set-up: I am working with some contractors. They
Let me start by saying that I do not advocate this approach, but I
Let me start off with a bit of background. This morning one of our
Let me start out by saying that I'm not a JavaScript developer so this
Let me start by telling you that I never used anything besides SVN and
First off, let me start off that I am not a .net developer. The

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.