drooh
drooh

Reputation: 678

avoid repeated mysql connections

Im not sure if by including this script in on every page load if I am re-connecting to the database.

Is there a way to just connect once? Should I use a session to tell me whether the site is connected?

Example: here is my config.php

<?
$dbhost = 'localhost';
$dbuser = 'user';
$dbpass = 'pass';
$db = 'demo';
$con = mysqli_connect($dbhost,$dbuser,$dbpass,$db);
?>

Then on index.php I have at the top

<?
session_start();
require_once 'inc/config.php';
include_once 'inc/functions.php';
?>

Each time index.php is loaded, will the db connect unnecessarily each time? If so, what is a good method to avoid this?

Upvotes: 1

Views: 313

Answers (1)

Your Common Sense
Your Common Sense

Reputation: 157862

Im not sure if by including this script in on every page load if I am re-connecting to the database.

Yes you are.

Is there a way to just connect once?

No.

Should I use a session to tell me whether the site is connected?

That's impossible.

There is nothing wrong with connecting on every page load. That's how it works. Nothing to be worry of.

Upvotes: 1

Related Questions