Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL connection: globally or in object?

I have two PHP classes. One is for connecting to the database, building queries, executing them, and disconnecting from the database. The other class is for users: adding them, updating them, logging them in, etc.

I'm debating whether I should connect to the database on the page globally and use that connection (passing the database object into the method of the user object), or whether I should connect and disconnect from the database from within the a user method itself.

The advantage I see for connecting globally, is that once connected I have that connection available for executing multiple methods. The disadvantage is that I 7need to worry about passing the database object around.

The advantage of connecting within the method is that it's completely transparent, however, there might be 4 or 5 connections established and disconnected, which may lead to an overhead.

Is there a best practice for using either, or does it really depend on the amount of users and the server's specs e.g. memory, cpu, etc. The system needs to support up to about 1,000 users, so it's fairly small scale.

Any feedback would be much appreciated.

-Ryan

like image 828
NightHawk Avatar asked Dec 08 '11 21:12

NightHawk


People also ask

What is connection object in MySQL?

A MySqlConnection object represents a session to a MySQL Server data source. When you create an instance of MySqlConnection, all properties are set to their initial values. For a list of these values, see the MySqlConnection constructor. If the MySqlConnection goes out of scope, it is not closed.

What are the ways the connection with MySQL can be closed by and connection objects as?

To close the connection in mysql database we use php function mysqli_close() which disconnect from database. It require a parameter which is a connection returned by the mysql_connect function. Syntax: mysqli_close(conn);

How do I create a database with connection object connection in MySQL?

To create a connection between the MySQL database and the python application, the connect() method of mysql. connector module is used. Pass the database details like HostName, username, and the database password in the method call. The method returns the connection object.

What is mysqlconnection in MySQL?

The MySqlConnection constructor takes a connection string as one of its parameters. The connection string provides necessary information to make the connection to the MySQL database. The connection string is discussed more fully in Section 5.1, “Connecting to MySQL Using Connector/NET”.

How do I connect to a MySQL database using a connector?

4.1.1 The MySqlConnection Object. For your MySQL Connector/NET application to connect to a MySQL database, it must establish a connection by using a MySqlConnection object. The MySqlConnection constructor takes a connection string as one of its parameters.

How do I connect to MySQL in Visual Studio?

To connect to MySQL. On the File menu, select Connect to MySQL (this option will be enabled after the creation of project). If you are previously connected to MySQL, the command name will be Reconnect to MySQL. In the Provider box, select MySQL ODBC 5.1 Driver (trusted). It is the default provider in the standard mode.

How to connect to MySQL from NET Core using C #?

How to Connect to MySQL from .NET Core. This tutorial will teach you how to connect to MySQL from .NET Core using C#. 1. Install MySqlConnector. First, install the MySqlConnector NuGet package. From a command prompt, run: Or right-click your project, choose Manage NuGet Packages…, in the Search box enter MySqlConnector, ...


2 Answers

In PHP, the best practice is to take the global approach. This is mainly due to the fact that repeated connects/disconnects to the MySQL server can cause a significant decrease in performance.

In fact, though this may seem counter-intuitive, most PHP experts (including myself) recommend that you avoid using mysql_close() altogether, unless there's a pressing reason not to. This is because that is handled automatically in PHP's cleanup anyway, so adding mysql_close() just creates a further deterioration in performance.

like image 151
Kris Craig Avatar answered Oct 14 '22 10:10

Kris Craig


Create a function that creates the connection on demand and provides access to the connection object:

function getDb()
{
    static $db;
    if (!$db) $db = ...;
    return $db;
}

Wrapped into class this approach is called Singleton

like image 36
zerkms Avatar answered Oct 14 '22 10:10

zerkms