Use the PDO method in PHP to open the connection to close the MySQL database

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.