Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beginner question on mySQL and PHP to avoid connecting to DB on every page

Tags:

sql

php

mysql

I code a simple php/mysql web page, that there is page1.php, page2.php and so on. Because I make use of the database on every page (or at least the 90% of them) I place on the top of them the standard

mysql_connect("localhost"," "," ");
mysql_select_db(" ");
.
.
mysql_close();

with my queries.

My question is do I really need to connect to the database on each page or is there any way to avoid this and still stay connected? Some of the pages are linked to the others and I can make use of SESSIONS to post some variables, but my question goes to something more globalized.

like image 947
EnexoOnoma Avatar asked Aug 23 '11 17:08

EnexoOnoma


People also ask

What is the correct way to connect to a MySQL database using PHP?

php $servername = "localhost"; $database = "database"; $username = "username"; $password = "password"; // Create connection $conn = mysqli_connect($servername, $username, $password, $database); // Check connection if ($conn->connect_error) { die("Connection failed: " .

Is it compulsory to have a MySQL database when building websites with PHP?

No. PHP is a programming language. MySQL is a database.

Which parameter is are compulsory to connect PHP to MySQL database?

Parameters ¶ e.g. "hostname:port" or a path to a local socket e.g. ":/path/to/socket" for the localhost. If the PHP directive mysql. default_host is undefined (default), then the default value is 'localhost:3306'. In SQL safe mode, this parameter is ignored and value 'localhost:3306' is always used.

What is the correct way to connect to a MySQL database?

To connect to the database server, confirm that the MySQL Database Server is running on your machine, right-click the Databases > MySQL Server node in the Services window and choose Connect. You might be prompted to supply a password to connect to the server.


3 Answers

The web works in a disconnected state by nature. Meaning that you have no idea if the client is going to come back for a second request or not.

Regardless you absolutely want to connect/disconnect from the database on every single page. This is the only way to ensure you aren't leaking connections and the site can stay responsive.

Most systems have built in ways to handle connection pooling which makes the act of requesting a new connection very very fast and therefore something you don't have to worry about.

like image 73
NotMe Avatar answered Oct 17 '22 04:10

NotMe


You can use mysql_pconnect for a persistent connection, although its not going to help you that much and it can be a big pain to do properly. Its almost just better to connect on every page, especially if the database server is running on the same machine as the php server.

like image 41
jli Avatar answered Oct 17 '22 05:10

jli


Try using

mysql_pconnect() 

From PHP.net

"acts very much like mysql_connect() with two major differences.

First, when connecting, the function would first try to find a (persistent) link that's already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.

Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use (mysql_close() will not close links established by mysql_pconnect())."

like image 1
swordfish Avatar answered Oct 17 '22 05:10

swordfish