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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T21:01:44+00:00 2026-05-27T21:01:44+00:00

I am a beginner in PHP programming and would like help with a little

  • 0

I am a beginner in PHP programming and would like help with a little question. Please take a look at the code below:


PHP Code

<?php
class Account
{
  public function register()
  {
    $db_link = mysql_connect("localhost","root","");  // Create Connection
    if (!$db_link)  // Check connection
    {
      die(mysql_error());
    }

    mysql_close($db_link); // Close Connection
  }

  public function login()
  {
    $con = mysql_connect("localhost","root","")  // create connection
    if (!$con)  // create connection
    {
      die(mysql_error());
    }
    mysql_close($con); //close connection
  }

}
?>

My question is if creating individual db links for every single one of the object’s methods is the best way to go? Is there a better or alternative way to do this? Hopefully I’ve explained well enough.


Would the following be correct?

$x = new Account("localhost", "root", "");

-and x would have its own connection…and then close when its done?

  • 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-27T21:01:45+00:00Added an answer on May 27, 2026 at 9:01 pm

    I would not advise creating your database connections this way. Create one connection and inject that into the object using it. You should not need to create a new connection for every object.

    Code example:

    $connection = new mysqli('localhost', 'user', 'password');
    
    $Account = new Account($connection);
    

    Would need to change Account to look like:

    class Account {
    
        protected $connection;
    
        public function __construct(mysqli $connection) {
            $this->connection = $connection;
        }
    
        public function register() {
            // use $this->connection for db
        }
    
        public function login() {
            // use $this->connection for db
        }
    
    }
    

    I would also suggest that you take a look at the php.net docs about choosing a MySQL API. If you really want to use OOP with PHP and MySQL you will need to swap over to mysqli or PDO as the API you are using does not truly support an OOP interface.

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

Sidebar

Related Questions

As I am a beginner in the PHP programming field, I would like to
I am a beginner at programming. I am writing code using class inheritance. Here
I am a beginner in PHP and would like to load a daily meditation
This is a topic that, as a beginner to PHP and programming, sort of
Beginner help needed :) I am doign an example form a php book which
I'm a beginner PHP coder, recently I've been told I indent my code not
I am editing somebody else's code and I am a PHP beginner. I want
I am currently a beginner in PHP OOP programming and was wondering if someone
I am a beginner to PHP and MySQL so please bear with me... I
I'm a beginner to PHP and I have a quick question. Is it possible

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.