The code is as follows:
1<meta http-equiv= "Content-type" content= "text/html" charset= "Utf-8"/>23<form method= "POST" action= "<?php Echo htmlspecialchars ($_server[' php_self '])? > ">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") {//get the submission method to compare and then use if to judge! - //Connect MySQL database with Mysqli Object-oriented method the $servername=$_post[' Mysqlip '];//Server Address - $username=$_post[' Mysqluser '];//User Account - $password=$_post[' Mysqlpassword '];//Password - $conn=NewMysqli ($servername,$username,$password);//instantiate Myqli, use object-oriented link MySQL database! + if($conn->connect_error) {//$conn->connect_error is represented by an object-oriented, error message returns a string, no error message returns NULL, and if the output information is judged; - Echo"Connection Failed! ".$conn->connect_error;//return NULL output +}Else{ A Echo"The database address you are connecting to is:".$servername; at Echo"<br/> the database account you are connected to is:".$username; - Echo"<br/> The database password you are connecting to is:".$password; - Echo"<br/> Connection was successful! ";//return string Output - } - if($conn-connect_error) { - Echo"<br/> not detected a connected database, so it's not closed! "; in}Else{ - $conn->close ();//object-oriented shut down the database! to Echo"<br/> has successfully closed the database connection! "; + } - } the?>
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;
Online test Instance Address: Http://blog.z88j.com/mysql/mysqli Object-oriented connection database. php
PHP with Mysqli Object-oriented open connection close MySQL database