The code is as follows:
1<meta http-equiv= "Content-type" content= "text/html" charset= "Utf-8"/>23<form method= "POST" action= "<?php Echo iconv (" GB2312 "," UTF-8 ", Htmlspecialchars ($_server[' php_self ']); > ">4Server address: <input type= "text" name= "Mysqlpdoip"/><br/>5Server account: <input type= "text" name= "Mysqlpdouser"/><br/>6Server password: <input type= "password" name= "Mysqlpdopassword"/><br/>7<input type= "Submit" value= "connect to MySQL"/>8</form>9<?PHPTen if($_server[' Request_method ']== "POST") {//If determine if post is submitted One $servername=$_post[' Mysqlpdoip ']; A $username=$_post[' Mysqlpdouser ']; - $password=$_post[' Mysqlpdopassword ']; - Try{ the //$conn = new PDO ("mysql:host= $servername;d bname=mydb", $username, $password); - $conn=NewPDO ("mysql:host=$servername;d Bname=mysql ",$username,$password);//connecting to a database - Echo"The connection was successful!" "; - Echo"<br/> the server address you are connected to:".$servername; + Echo"<br/> the server account you are connected to:".$username; - Echo"<br/> password of the server you are linking:".$password; + $conn=NULL;//To close a database connection A Echo"<br/> successfully closed database"; at } - Catch(pdoexception$e) - { - Echo $e-getMessage (); - } - } in?>
Try
{ //...}
catch (Exception $e)
{ //...}
try{}catch{} in PHP is exception handling.
The code that will be executed is put into a try block, and if an exception occurs during the execution of the code, the program jumps directly into the catch block and the $e collects the error information and displays it.
If an exception is thrown, the script in the Try statement stops executing and then immediately turns to the script in the Execute Catch statement.
If the exception is thrown and is not captured, a fatal error is generated.
Instance Test Address: Http://blog.z88j.com/mysql/PDO method connect MySQL database. php
Use the PDO method in PHP to open the connection to close the MySQL database