9951 explained code solutions for 126 technologies


php-mysqlHow to create a connection pool in a MySQL database using PHP?


Creating a connection pool in a MySQL database using PHP is a great way to improve the performance of your application. It allows you to reuse existing connections instead of creating a new one each time a query is executed.

$db_host = 'localhost';
$db_user = 'username';
$db_pass = 'password';
$db_name = 'database_name';

$db_options = array(
    PDO::ATTR_PERSISTENT => true
);

$db_connection = new PDO("mysql:host=$db_host;dbname=$db_name", $db_user, $db_pass, $db_options);

The example code above creates a connection pool to a MySQL database using PHP. It sets the PDO::ATTR_PERSISTENT attribute to true which tells PDO to use an existing connection if one exists.

  1. $db_host: The hostname of the MySQL server.
  2. $db_user: The username used to connect to the MySQL server.
  3. $db_pass: The password used to connect to the MySQL server.
  4. $db_name: The name of the database to connect to.
  5. $db_options: An array of options to pass to the PDO constructor.
  6. $db_connection: The PDO object used to connect to the MySQL server.

Helpful links

Edit this code on GitHub