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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:11:40+00:00 2026-05-12T17:11:40+00:00

So recently I’ve really started to use php actively, and I need some insights

  • 0

So recently I’ve really started to use php actively, and I need some insights on different ways to use database connections.

At first I just used the simple mysql_connect():

<?php
    $connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
    mysql_select_db(DB_DB, $connection);
?>

After a while I created a database class which I started to include and initialize in every file – something like this:

<?php
class MySQL_DB {

var $connection;

function MySQL_DB(){
    $this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
    mysql_select_db(DB_DB, $this->connection);
}

function query($q){
    $res = mysql_query($q, $this->connection) or die(mysql_error());
    return $res;
}
}

$database = New MySQL_DB;
?>

And this is what I’m using at the time – and it’s working fine – but there are always ways to improve.

So my question to you is how do you manage your database connections?

  • Do you use classes?
  • What does your classes contain (just
    the connection or even functions?)
  • What practices do you recommend?
  • 1 1 Answer
  • 3 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-12T17:11:41+00:00Added an answer on May 12, 2026 at 5:11 pm

    Usage of classes are the way to go to increase customized re-usability.

    Bring in all generic implementations into the class. You are on the right track.

    This website has the following clean approach.
    This website link is no longer present. Archive Link.

    class connection {
        // Possible Modules are as follows:
        // DBX_MYSQL, DBX_ODBC, DBX_PGSQL, DBX_MSSQL, DBX_FBSQL, DBX_SYBASECT, DBX_OCI8, DBX_SQLITE
        private $module = DBX_MYSQL; 
    
        private $host = "localhost";
        private $database = "test";
        private $username = "testuser";
        private $password = "testpass";
    
        private $link;
        private $result;
        public $sql;
    
        function __construct($database=""){
                if (!empty($database)){ $this->database = $database; }
                $this->link = dbx_connect($this->module,$this->host,$this->database,$this->username,$this->password);
                return $this->link;  // returns false if connection could not be made.
        }
    
        function query($sql){
                if (!empty($sql)){
                        $this->sql = $sql;
                        $this->result = dbx_query($this->link,$sql,DBX_RESULT_UNBUFFERED);
                        return $this->result;
                }else{
                        return false;
                }
        }
    
        function fetch($result=""){
                if (empty($result)){ $result = $this->result; }
                return dbx_fetch_row($result);
        }
    
        function __destruct(){
                dbx_close($this->link);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently the PHP manual started showing the following warning on every mysql function page:
Recently, I started changing some of our applications to support MS SQL Server as
Recently my records started to disappear from my application's database so I want to
Recently I was asked to develop an app, which basically is going to use
Recently, I was asked to compile some of my Android projects, based on the
Recently I'm doing some work on RTMP streaming, that is using Flowplayer to integrate
Recently I have been dealing with windows LogonUser API. The LogonUser api returns different
recently I started using storyboard and I've the following situation: I want to set
Recently two users of our software from the same company started experiencing random closures
Recently, we discovered odd behavior in some old code. This code has worked for

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.