Skip to content
Advertisement

how to close a connection to a database from a response of a fetch API?

I have a file where I setup a connection to Mysql:

class Connection extends Mysqli {
    function __construct() {
        parent::__construct('localhost','root','','prueba');
        $this->set_charset('utf8');
        $this->connect_error == NULL ? 'DB Conectada' : die('Error al conectarse a la base de 
                                                              datos');
    }
}

And in a script I have set a fetch() where in the response I want to close the existing connection if dataU.data['cod_tipo_usu'] == 1 because it’s a completely new page that has a register form and it doesn’t have relation with the connection with the database.

.then(dataU => {
                if (dataU.status) {
                    // console.log(dataU.data['cod_tipo_usu']);
                    frm_login.reset();
                    if (dataU.data['cod_tipo_usu'] == 1) {
                        location.href = 'v_ingresoP.php';   
                        // code to close the connection
                    } else {
                        location.href = 'index.php';
                    }
                }

I did tried $this->close() in the function of Connection class but everything in the index.php closed when I refreshed the navigators view.

Any idea on how to close the connection?

Advertisement

Answer

It doesn’t work like that. JavaScript is executed on the client-side (in the web browser) and PHP on the server-side. Every time you make a request to the server, the PHP script is executed from scratch. The connection is established on every request and closed when the request completes.

You can’t close mysqli connection from JavaScript because this is impossible. Don’t worry about closing connections anyway. PHP will close them automatically when the request is complete. It’s not something you have to do manually.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement