


How do PHP and MySql implement reading of background data? (Code)
There are many ways to realize background data reading. Next, I will share with you PHP MySql to realize background data reading. Here, we use PHP’s php_mysqli extension. So, first understand some basics. How to use it.
1. Connect to the database using
mysqli_connect()
Parameters: ① Host address ② MYSQL user name ③ MYSQL password ④ Select the database to connect to ⑤ Port number
Return: Return if the connection is successful The identifier of the resource type. If it fails, it returns false
If we establish more than one connection with Mysql, then various functions that operate the database in the future must pass in the returned connection symbol
If we establish only one connection with Mysql One, then there is no need to pass in this identifier to various functions that operate the database in the future
2. Set the character set encoding format
mysqli_set_charset();
3. Execute the SQL statement
If it is addition, deletion or modification, it will return the success of Boolean type
If it is a query, it will return the resource result set
$res=mysqli_query();
4. Process the result set
mysqli_fetch_assoc($res); Return associative array
mysqli_fetch_row($res); Return index array
mysqli_fetch_object($res); Return object
mysqli_fetch_field($res); Return the field information of each column in the result set (Field name, table name, database name, field type)
mysqli_data_seek($res, 0); Set the result set pointer position to zero, and reset the result set to the beginning
mysqli_free_result( $res); Release the query resource result set
mysqli_close($conn); Close the database connection
Let’s implement a simple registration and login function.
First mention the general configuration into a PHP file, and then import it later
<?php header("Content-Type:text/html;charset=utf-8"); define("HOST", "127.0.0.1"); define("USERNAME", "root"); define("PASSWORD", ""); define("DBNAME", "mydb"); define("CHARSET", "utf8"); $con=mysqli_connect(HOST, USERNAME, PASSWORD, DBNAME) or die("数据库连接失败,<span style='color:red;'>".mysqli_connect_error()."</span>"); mysqli_set_charset($con, CHARSET) or die("字符集编码设置无效");
Create a table in the database to access user information. Here I have created a table named submit in mydb database.
The first thing to do is to register the function. Registration is to save the information in the user input form into the table of the background database.
The following is the style of the registration page, nothing Easy to say, just remember the name ID<p class="panel panel-primary"> <p class="panel-heading"> <p class="panel-title">用户注册</p> </p> <p class="panel-body"> <form class="form-horizontal"> <p class="form-group"> <label>用户名</label> <input type="text" class="form-control" name="userName"/> </p> <p class="form-group"> <label>密码</label> <input type="password" class="form-control" name="pwd" /> </p> <p class="form-group"> <label>确认密码</label> <input type="password" class="form-control" name="rePwd" /> </p> <p class="form-group"> <label>真实姓名</label> <input type="text" class="form-control" name="realName" /> </p> <p class="form-group btns"> <input type="button" class="btn btn-primary" value="确定注册" id="submit"/> <a type="button" class="btn btn-success" href="login.php"/>返回登录</a> </p> </form> </p> </p>
The key point is to see how to use JQuery to POST data to the background
<script src="../../js/jquery-1.10.2.js"></script> <script type="text/javascript"> $(function(){ $("#submit").on("click",function(){ var userName = $("input[name='userName']").val(); var pwd = $("input[name='pwd']").val(); var rePwd = $("input[name='rePwd']").val(); var realName = $("input[name='realName']").val(); if(userName==""||pwd==""||rePwd==""||realName==""){ alert("所有信息不可为空,请确认!"); return; }else if(pwd!=rePwd){ alert("两次密码输入不一致!"); return; } $.post("doReg.php",{ "userName":userName, "pwd":pwd, "realName":realName },function(data){ alert(data); if(data=="注册成功"){ location = "login.php"; } }) }); }); </script>
After the background php file receives the data, it will operate the database through SQL statements and store the data in the table
<?php header("Content-Type:text/html;charset=utf-8"); $str=$_POST["formData"]; list($username)=explode("&", $str); list(,$pwd)=explode("&", $str); list(,,,$realname)=explode("&", $str); list(,$username)=explode("=", $username); list(,$pwd)=explode("=", $pwd); list(,$realname)=explode("=", $realname); include_once("mysql.php"); $sql=<<<sql insert into submit (username,pwd,realname) values ("$username","$pwd","$realname"); sql; $sql2=<<<sql2 SELECT username FROM submit WHERE username="$username"; sql2; $res2=mysqli_query($con, $sql2); $res=mysqli_query($con, $sql); if(mysqli_num_rows($res2)>0){ die("用户名已经存在!"); } elseif($res){ echo 'true'; }else{ die(); }
In this way, click the registration button to save the entered information into the table. After success, jump to the login page
The following is the login page. The login function needs to read the user name and password information stored in the table
Login page Not much to say about the style. Also remember the required name and ID
<p class="panel panel-primary"> <p class="panel-heading"> <p class="panel-title">用户登录</p> </p> <p class="panel-body"> <form class="form-horizontal"> <p class="form-group"> <label>用户名</label> <input type="text" class="form-control" name="userName"/> </p> <p class="form-group"> <label>密码</label> <input type="password" class="form-control" name="pwd"/> </p> <p class="form-group btns"> <input type="button" class="btn btn-primary" value="登录系统" id="submit"/> <a type="button" class="btn btn-success" href="reg.php"/>注册账号</a> </p> </form> </p> </p>
The key point is the JQ code
<script src="../../js/jquery-1.10.2.js"></script> <script type="text/javascript"> $(function(){ $("#submit").on("click",function(){ var userName = $("input[name='userName']").val(); var pwd = $("input[name='pwd']").val(); $.post("doLogin.php",{ "userName":userName, "pwd":pwd },function(data){ alert(data); if(data=="登录成功"){ location = "index.php"; }else{ alert("用户名或密码有误!"); } }); }); }); </script>
The JQ code of the above landing page What is done is to get the user name and password information read from the database in the background login PHP file and compare it with the new input by the user. If true, the login is successful. Then the background login page How to write it is very simple. After reading the information from the table through the SQL statement, it can be returned to the front desk login page
Related recommendations: The above is the detailed content of How do PHP and MySql implement reading of background data? (Code). For more information, please follow other related articles on the PHP Chinese website!<?php
header("Content-Type:text/html;charset=utf-8");include_once("../mysql/mysql.php");
$userName = $_POST["userName"];
$pwd = $_POST["pwd"];
$loginSql = <<<login
select * from submit where username="{$userName}" and pwd = "{$pwd}";
login;
$res = mysqli_query($con, $loginSql);
if($row = mysqli_fetch_row($res)){
$_SESSION["user"] = $row;
echo "登录成功";
}else{
echo "登录失败";
}
mysqli_free_result($res);
mysqli_close($con);

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

There are four main error types in PHP: 1.Notice: the slightest, will not interrupt the program, such as accessing undefined variables; 2. Warning: serious than Notice, will not terminate the program, such as containing no files; 3. FatalError: the most serious, will terminate the program, such as calling no function; 4. ParseError: syntax error, will prevent the program from being executed, such as forgetting to add the end tag.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

HTTP request methods include GET, POST, PUT and DELETE, which are used to obtain, submit, update and delete resources respectively. 1. The GET method is used to obtain resources and is suitable for read operations. 2. The POST method is used to submit data and is often used to create new resources. 3. The PUT method is used to update resources and is suitable for complete updates. 4. The DELETE method is used to delete resources and is suitable for deletion operations.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

The arrow function was introduced in PHP7.4 and is a simplified form of short closures. 1) They are defined using the => operator, omitting function and use keywords. 2) The arrow function automatically captures the current scope variable without the use keyword. 3) They are often used in callback functions and short calculations to improve code simplicity and readability.
