Will the MySQL connection remain open in the code below after we leave the script page?

$mysqli = new mysqli("localhost", "root", "", "mydb"); $mysqli -> query("SET NAMES 'utf8'"); // манипулируем с БД echo "<script type=\"text/javascript\"> window.location.href = \"http://localhost/oterpage.php\" </script>"; $mysqli -> close(); 

I made the redirect via JS, because before this code I already had output to the browser and because of this the header cannot be used.

  • After the script is fully completed, php will kill the connection to the database. To mine he will do it without $ mysqli -> close (); - heff

1 answer 1

PHP code is executed after the server receives an HTTP request. After the request has been fully received and transmitted to the PHP interpreter, the client’s browser can in no way affect the execution of the PHP code. All he can do is make her one request. So, after the interpreter makes an echo it makes $mysqli -> close(); which will lead to the closure of the database connection.

  • Excellent explained everything! Thank you for the answer! - Side Gleb