PHP with Mysqli Object-oriented open connection close 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 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

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.