


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
<?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);
Related recommendations:
php small practical recommendation: php mysql to implement login and Query function code exampleThe 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!

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft