Adam Adam - 1 year ago 78
SQL Question

PHP Database Connection - Calling Multiple Times

I'm using the following code to connect to my Database - PHP7 to MySQL



The connection works fine.

My question is I'm calling this code around 5 times for each page request. This is because its included different functions.

Does this mean I'm making 5 DB connects for each page request? Or is it persistent and only called once?

If it is calling multiples times I could use a global to keep it to once.


Answer Source

You are using object, which creates connection in constructor. You will have as many connections as instances of database will be created.

  1. If you create database instance, set it to some variable and use connection from this instance - there will be only one connection.

  2. If you are creating database instance for each query - there will be many connections.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download