I load the page. There is a connection to the database to download certain data (I apologize for the tautology). Then when you press the button, the php script is executed in which there is a query to the database.

Do I understand correctly that in the last script I will have to connect to the database again?

Is it right to do this or should (you can) connect once and not repeat?

    2 answers 2

    According to the code, you need to connect every time.

    But there are a couple of moments:

    1) Worked on the application where, after rebooting the database, it was necessary to overload php-fpm / lighttpd ( lighttpd / php5-fpm / pdo / postgres ). Those. It looks like it was either cached or not closed.

    2) Functions with probable connections exist. http://php.net/manual/en/features.persistent-connections.php

    The essence of PHP is that it quickly worked and forgotten. Therefore, in fact, it is not a problem to open for each access to the server connection to the database. The main thing is not to open the connection for each request that you execute in the script. And what would the script / query run quickly.

    And if you want to optimize:

    1. Optimize requests ( explain in help).
    2. See caching memcahced , redis ...
    • Thank you, that's the case. I will study - Zhenya Vedenin

    That you came up with fun.
    You connect a php-file with executable code, but in it already connect to the database. Separately it does not need.

    • Apparently somehow I crookedly explained (Is it necessary to go on this connected php? - Zhenya Vedenin
    • So you yourself and check if there is no sampling and making mistakes, then try it. - tvset
    • It does not go, I go - it goes. Therefore, the question asked "Is it right to do this or should (you can) connect once and not repeat?" - Zhenya Vedenin
    • yes enough once. - tvset