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 ']); /using the Htmlspecialchars () function to?> the output of the ">4Server address: <input type= "text" name= "Mysqlip"/>5<br/>6Server account: <input type= "text" name= "Mysqluser"/>7<br/>8Server password: <input type= "password" name= "Mysqlpassword"/>9<br/>Ten<input type= "Submit" value= "connect to MySQL"/> One</form> A<?PHP - if($_server[' Request_method ']== "POST") {//If the page is submitted as a post - $servsername=$_post[' Mysqlip ']; the $username=$_post[' Mysqluser ']; - $password=$_post[' Mysqlpassword ']; - Mysqli_connect($servsername,$username,$password); - if(Mysqli_connect_error()){//If you determine if the link is successful! + Echo"Connection Failed! ".Mysqli_connect_error(); -}Else{ + Echo"The connection was successful!" "; A Echo"<br/> The database address you are connected to:".$servsername; at Echo"<br/> the database account you are connected to:".$username; - Echo"<br/> The database password you are connected to:".$password; - } - if(Mysqli_connect_error()){//If you determine the link succeeds, then close the database! - Echo<br/> the database being connected is not detected! Unable to close the database! "; -}Else{ in Echo"<br/> has closed the database connection! "; - } to } +?>
The first if judge whether the page has been post method to submit data, to prevent the error when assigning a value;
The second if the database is successfully connected to the error, the client is judged to return;
The third if the database has been successfully connected error, the decision to close the connection;
Also, some servers will have to submit not to go up, here we do code conversion, the page is UTF-8 we put $_server[' php_self '] the path obtained by default may be GB2312, with iconv function encoding conversion utf-8!
Online test Instance Address: Http://blog.z88j.com/mysql/mysqli process-oriented connection database. php
PHP with mysqli-oriented process open connection close MySQL database