PHP mysql connection is closed

PHP mysql connection

In fact: PHP itself can not operate mysql, but there is Php extension can be achieved mysql: PHP extensions to implement the aid operation Mysql

Extended operation mysql php are: mysql, mysqli and PDO extension

mysql extension: pure process-oriented, there is a function, after loading the extension can call the function. (Currently only used for the process)
the mysqli Extension: + object-oriented process-oriented, there is also a function of the class, after loading or extension may choose to call the function call type operation.
Use only pure object-oriented class, only class, after loading: PDO.

mysql extension when building the server has been loaded on, do not load the extensions.

PHP mysql operations
after when php to operate the Mysql: PHP's role is a client of Mysql.
Client server operation necessary process
1, MySQLi - Object Oriented connection

 <?php
 header("Content-type:text/html;charset=utf-8");
 $servername = "localhost";
 $username = "root";
 $password = "root";

 // 创建连接
$conn = new mysqli($servername, $username, $password);

// 检测连接
if ($conn->connect_error) {
die("连接失败: " . $conn->connect_error);
}
 echo "连接成功";
?>

Here Insert Picture Description

2, MySQLi - connection-oriented procedure

<?php
$servername = "localhost";
$username = "username";
$password = "password";

// 创建连接
$conn = mysqli_connect($servername, $username, $password);

// 检测连接
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "连接成功";
?>

Here Insert Picture Description
3, PDO connection

<?php
$servername = "localhost";
$username = "username";
$password = "password";

try {
$conn = new PDO("mysql:host=$servername;", $username, $password);
echo "连接成功"; 
}
      catch(PDOException $e)
{
echo $e->getMessage();
}
 ?>

Here Insert Picture Description

PHP closes the connection

1, MySQLi - Object Oriented

$conn->close();

2, MySQLi - process-oriented

mysqli_close($conn);

3、PDO

$conn = null;

Guess you like

Origin blog.csdn.net/weixin_44097082/article/details/95020690