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);