PHP | mysqli_close() Function Last Updated : 13 May, 2019 Comments Improve Suggest changes Like Article Like Report MySQLi Procedural procedure: To close the connection in mysql database we use php function mysqli_close() which disconnect from database. It require a parameter which is a connection returned by the mysql_connect function. Syntax: mysqli_close(conn); If the parameter is not specified in mysqli_close() function, then the last opened database is closed. This function returns true if it closes the connection successfully otherwise it returns false. Below program illustrate the mysqli_close() function PHP <?php $servername = "localhost"; $username = "username"; $password = "password"; // Creating connection $conn = mysqli_connect($servername, $username, $password); // Checking connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // Creating a database named newDB $sql = "CREATE DATABASE newDB"; if (mysqli_query($conn, $sql)) { echo "Database created successfully with the name newDB"; } else { echo "Error creating database: " . mysqli_error($conn); } // closing connection mysqli_close($conn); ?> MySQLi Object-oriented procedure:: To close the connection in mysql database we use php function conn->close() which disconnect from database. Syntax: conn->close(); Program: To illustrate the closing of connection in object-oriented procedure. PHP <?php $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "newDB"; // checking connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } //Close the connection $conn->close(); ?> Using PDO procedure: To close the connection in MySQL database in PDO procedure we set the connection name to null which disconnect from the database. Syntax: conn=null; Program: to illustrate the closing of connection in PDO procedure. PHP <?php $servername = "localhost"; $username = "username"; $password = "password"; try { $conn = new PDO("mysql:host=$servername;dbname=newDB", $username, $password); // setting the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); $sql = "CREATE DATABASE newDB"; // using exec() because no results are returned $conn->exec($sql); echo "Database created successfully with the name newDB"; } catch(PDOException $e) { echo $sql . " " . $e->getMessage(); } $conn = null; ?> References: http://php.net/manual/en/mysqli.close.php Comment More infoAdvertise with us Next Article PHP | mysqli_close() Function K KRV Follow Improve Article Tags : Web Technologies PHP Similar Reads PHP mysqli_connect() Function The mysqli_connect() function in PHP is a fundamental tool for establishing a connection to a MySQL database. This function is crucial for PHP applications that need to interact with MySQL databases, enabling them to execute queries, retrieve data, and perform various database operations.In this art 3 min read PHP | mysqli_error() Function The mysqli_error() function is used to return the error in the most recent MySQL function call that failed. If there are multiple MySQL function calls, the error in the last statement is the one that is pointed out by the function. Syntax: mysqli_error("database_name") Parameters: This function acce 1 min read PHP fclose() Function The fclose() function in PHP closes a file that was previously opened by fopen(). Closing a file releases the resource associated with it and makes sure all the data written to the file is properly saved. Not closing files can lead to resource leaks or incomplete data writing.Syntax:bool fclose(reso 2 min read PHP | mysqli_ping() Function The mysqli_ping() function is used to ping a server connection. That is it is used to check if a host is reachable on an IP network or not. This function also tries to reconnect if an existing server connection is lost. To use this function, it is mandatory to first set up the connection with the My 2 min read PHP | closedir( ) Function The closedir() function in PHP is an inbuilt function which is used to close a directory handle. The directory handle to be closed is sent as a parameter to the closedir() function and the closedir() closes the directory handle. The directory handle must be previously opened by the opendir() functio 2 min read Like