FARSOS BULSARA FARSOS BULSARA - 6 months ago 13
PHP Question

Continously enter data without closing sql connection

Please forgive me for this question, I am very new to PHP and I can't find the answer to my question anywhere.

I need to update data from the user to the server continuously at a timed internal which I have set up using javascript and AJAX.

Is there anyway to set up php so that once it connects to the server, it will keep the connection open and continue to accept new data until I tell it to close?

Answer

You could set up a persistent connection to MySQL:

<?php
  $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass, array(PDO::ATTR_PERSISTENT => true
  ));
?> 

However, persistent connections in a web application can create serious issues. See What are the disadvantages of using persistent connection in PDO topic here on SO for a detailed discussion on persistent database connections.

In MySQL there is no significant overhead associated with creating and closing database connections, so just establish a new connection every time you call your php script.