MySQLi程序過程:
要關閉mysql數據庫中的連接,我們使用php函數mysqli_close()從數據庫斷開連接。它需要一個參數,該參數是mysql_connect函數返回的連接。
用法:
mysqli_close(conn);
如果未在mysqli_close()函數中指定該參數,則將關閉最後打開的數據庫。如果成功關閉連接,此函數將返回true,否則返回false。
下麵的程序演示了mysqli_close()函數
<?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麵向對象的過程::
要關閉mysql數據庫中的連接,我們使用php函數conn-> close()從數據庫斷開連接。
用法:
conn->close();
程序:為了說明在麵向對象的過程中關閉連接。
<?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();
?>
使用PDO程序:
要在PDO過程中關閉MySQL數據庫中的連接,我們將連接名稱設置為null,該名稱與數據庫斷開連接。
用法:
conn=null;
程序:說明在PDO程序中連接的關閉。
<?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;
?>
參考文獻: http://php.net/manual/en/mysqli.close.php
相關用法
- p5.js day()用法及代碼示例
- PHP dir()用法及代碼示例
- PHP each()用法及代碼示例
- PHP each()用法及代碼示例
- p5.js second()用法及代碼示例
- p5.js int()用法及代碼示例
- d3.js d3.max()用法及代碼示例
- PHP Ds\Map put()用法及代碼示例
- p5.js str()用法及代碼示例
- p5.js arc()用法及代碼示例
- d3.js d3.hcl()用法及代碼示例
- d3.js d3.lab()用法及代碼示例
注:本文由純淨天空篩選整理自KRV大神的英文原創作品 PHP | mysqli_close() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。