How to connect a mysql database with mysqli series functions?
1. PHP mysqli connect() function: mysqli connect() function opens a new connection to MySQL server.
Syntax: mysqli connect (host (specify host name or IP address), username(mysql user name), password(mysql password), dbname (default database))
Open a new connection to the MySQL server:
<?php $con = mysqli_connect("localhost","wrong_user","password","my_db"); if(!$con) { die( "Connection error:".mysqli_connect_error() ); } ?>
2. PHP mysqli query() function: execute a query for the database.
Syntax: mysqli_query('connection' (required). Specify the MySQL connection to use.) , query (required, specifies the query string, that is, the query statement.) ,resultmode);
Select database:
<?php //Suppose the database user name: root, password: 123456, database: mengli $con = mysqli_connect("localhost","root","123456","mengli"); $sql = "SELECT*FROM user WHERE username = '".$_POST['username']."'"; if( mysqli_connect_errno($con) ){ echo "Connect MySql Failure:".mysqli_connect_error(); } //Execution query mysqli_query($con,$sql); //To close a previously open database connection: mysqli_close($con); ?>
Execute a certain
3. PHP mysqli? Fetch? All() function: get all rows from the result set as an associated array, or a number array, or both. Note: this function is only available with MySQL Native Driver.
Syntax: mysqli? Fetch? All (result (required). Specifies the result set identifier to be returned by mysqli ﹣ query(), mysqli ﹣ store ﹣ result(), or mysqli ﹣ use ﹣ result(). ), resulttype (specifies which type of array should be generated);
<?php //Suppose the database user name: root, password: 123456, database: mengli $con = mysqli_connect("localhost","root","123456","mengli"); $sql = "SELECT*FROM user WHERE username = '".$_POST['username']."'"; if( mysqli_connect_errno($con) ){ echo "Connect MySql Failure:".mysqli_connect_error(); } //Execution query $result = mysqli_query($con,$sql); // get data mysqli_fetch_all($result,MYSQLI_ASSOC); // Release result set mysqli_free_result($result); //To close a previously open database connection: mysqli_close($con); ?>
First, create an index.html file (form)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Document</title> </head> <body> <form action="index.php" method="post"> User name:<input type="text" name="username"> Password:<input type="password" name="password"> <button type="submit">Submission</button> </form> </body> <script type="text/javascript"> </script> </html>
index.php file
<?php $db = []; $db["host"] = "127.0.0.1"; $db["user"] = "root"; $db["pwd"] = "root"; $db["database"] = "mengli"; $con = mysqli_connect($db['host'], $db['user'], $db['pwd'], $db['database']); $sql = "SELECT * FROM user WHERE username = '" . $_POST['username'] . "'"; $result = mysqli_query($con, $sql); $result = mysqli_fetch_all($result); //Check whether the acquired data is consistent //If the obtained data is empty, the user name entered by the user does not exist if ( empty( $result ) ) { echo "This user could not be found";//If the obtained data is empty, the user name entered by the user does not exist die; } if( !empty( $result ) ){//If the acquired data is not empty //Check whether the password entered by the customer is consistent with the password in the data obtained if ($result[0][2] == $_POST["password"]) { echo "Login successfully"; die; } else { echo "Password error"; die; } } // Release result set mysqli_free_result($result); //To close a previously open database connection: mysqli_close($con);