catalogue
Establish and close the connection with MySQL server
1) Connect to the specified mysql server
2) Prompt in case of connection error
3) Set default character encoding
4) Select a specific database: mysqli_select_db ( $link , string $dbname);
5) Close the connection with mysql server
2, Realize login registration form
1, Basic knowledge
Interaction between PHP and mysql_ pipasound blog - CSDN blog This article is very good
Establish and close the connection with MySQL server
1) Connect to the specified mysql server
$link=@mysqli_connect($host, $user, $password,$database,$port);
2) Prompt in case of connection error
int mysqli_connect_errno (); / / return the error code of the last connection call
string mysqli_connect_error (); // Returns the error code of the last connection call described by a string
3) Set default character encoding
mysqli_set_charset ( $link , string $charset )
4) Select a specific database: mysqli_select_db ( $link , string $dbname);
5) Close the connection with mysql server
mysqli_close ( $link );
<?php header('Content-type:text/html;charset=utf-8'); //Establish connection with MySQL database $link = @mysqli_connect('localhost', 'root' . '', 'login'); //Prompt in case of connection error if (mysqli_connect_errno()) { exit(mysqli_connect_error()); } //Set default character encoding mysqli_set_charset($link, 'utf8'); //Select a specific database mysqli_select_db($link, 'login'); //Do all kinds of operations! //Close the connection with mysql server mysqli_close($link);
2, Realize login registration form
First of all, I created a login library user table locally, including the user name and password fields (these are the two items in my form)
First of all, HTML and PHP are written for login and registration here, which will be introduced below

Login
If there is a login interface, you have to write HTML first (no rendering, very scrawly)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Dengliang front end</title> </head> <body> <div> <form action="login.php" method="POST"> user name:<input type="text" name="username"><br> password: <input type="password" name="password"><br> <input type="submit" value="Sign in"><br> </form> <div> No account yet?<a href="register.html">Register now</a> </div> </div> </body> </html>
The next step is php interactive mysql, which uses the above knowledge to connect with HTML through the action attribute of the form
<?php header('Content-type:text/html;charset=utf-8'); //Establish connection with MySQL database $link = @mysqli_connect('127.0.0.1', 'root' . ''); //Prompt in case of connection error if (!$link) { die("connection failed: " . mysqli_connect_error()); } //Set default character encoding mysqli_set_charset($link, 'utf8'); //Select a specific database mysqli_select_db($link, 'login'); $username = $_POST['username']; $password = $_POST['password']; if ($username == '') { echo "<script>alert('enter one user name')</script>"; } if ($password == '') { echo "<script>alert('Please input a password')</script>"; } //Database query statement is such a query method, which has fatal SQL injection $sql = "select * from user where username='$username' and password='$password'"; //The query results are saved in the $res object $res = mysqli_query($link, $sql); //Convert $res into an indexed array $row = mysqli_fetch_array($res, MYSQLI_NUM); // If the array is not empty, the login is successful if (!is_null($row)) { echo "Login successful"; } else { echo "Wrong account or password<br>Login failed!"; } //Close the connection with mysql server mysqli_close($link);
Casually enter the user name and password, and the login fails, because the database is empty and there is no registration
Register
Because there is no registration, you can directly access the register through the a link to register now Html is the registration front end
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Registration front end</title> </head> <body> <div> <form action="register.php" method="post"> <p><span>user name:<input type="text" name="username" required=""></span></p> <p><span>password:<input type="text" name="password" required=""></span></p> <input type="reset" name=""><input type="submit" name="Submit" value="register"> </form> <a href="login.html">Registered</a>> </div> </body> </html>
Register php to interact with mysql and insert data through insert into, which is called registration
<?php header('Content-type:text/html;charset=utf-8'); $link = mysqli_connect('localhost', 'root', ''); if (!$link) { die("connection failed" . mysqli_connect_error()); } mysqli_set_charset($link, 'utf8'); mysqli_select_db($link, 'login'); $username = $_POST['username']; $password = $_POST['password']; $sql = "INSERT INTO user values('{$username}' , '{$password}')"; $res = mysqli_query($link, $sql); if (!$res) { die("cannot insert " . mysqli_error($link)); } echo "login was successful<br>"; echo "<a href='login.html'>Sign in</a>"; mysqli_close($link);
We write data registration and execute it successfully
At this time, the corresponding data is also inserted into our user table. If the registration is successful, we can log in directly
You can find that the login is successful