Home >Backend Development >PHP Problem >How to read query data from mysql database in php

How to read query data from mysql database in php

藏色散人
藏色散人Original
2021-10-18 09:22:283104browse

php method to read queried data from the mysql database: 1. Connect to the mysql database through mysqli_connect; 2. Set the character set encoding format; 3. Execute the SQL statement; 4. Process the result set.

How to read query data from mysql database in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How to read php from the mysql database Query data?

PHP MySql implements background data reading:

We use the php_mysqli extension of PHP

First understand some basic usage

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 Boolean type whether it is successful or not
If it is a query, it will return the resource result set
$res=mysqli_query();

4. Process the result set

    mysqli_fetch_assoc($res);   返回关联数组
    mysqli_fetch_row($res);    返回索引数组
    mysqli_fetch_object($res);   返回对象
    mysqli_fetch_field($res);      返回结果集中每一列的字段信息(字段名,表名,数据库名,字段类型)
    mysqli_data_seek($res, 0);    设置结果集指针位置,为零,结果集复位到最开始
    mysqli_free_result($res);     释放查询资源结果集
    mysqli_close($conn);      关闭数据库连接

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=&#39;color:red;&#39;>".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 entered by the user into the table in the background database

The following is the style of the registration page, there is nothing to say, just remember the name ID

<p>d637b26c7618e8e4a387936d13117b79<br>            41f694bf073fda5f8b66ecb924074ffb<br>                62adb46bd367e8d79708a3bf3765ea34用户注册16b28748ea4df4d9c2150843fecfba68<br>            16b28748ea4df4d9c2150843fecfba68<br>            165eec484993f6c5e1b1fbd7bf5de649<br>                e61fd69228ce2c22fa7ddf26c584f7f0<br>                    b48d3695334c0d29c002309400bcc6ac<br>                        2e1cf0710519d5598b1f0f14c36ba674用户名8c1ecd4bb896b2264e0711597d40766c<br>                        3539a64e167199a3df90fef036934dd2<br>                    16b28748ea4df4d9c2150843fecfba68<br>                    b48d3695334c0d29c002309400bcc6ac<br>                        2e1cf0710519d5598b1f0f14c36ba674密码8c1ecd4bb896b2264e0711597d40766c<br>                        e9fc16704f34efbb11b15fe4cb6374de<br>                    16b28748ea4df4d9c2150843fecfba68<br>                    b48d3695334c0d29c002309400bcc6ac<br>                        2e1cf0710519d5598b1f0f14c36ba674确认密码8c1ecd4bb896b2264e0711597d40766c<br>                        d73581b0707fe32523581bf1842a8f07<br>                    16b28748ea4df4d9c2150843fecfba68<br>                    b48d3695334c0d29c002309400bcc6ac<br>                        2e1cf0710519d5598b1f0f14c36ba674真实姓名8c1ecd4bb896b2264e0711597d40766c<br>                        8ce8835b7396c2dfb5defcf6686ff330<br>                    16b28748ea4df4d9c2150843fecfba68<br>                    <br>                    52f158f9b58d170d0b8c1792170e9180<br>                        13d9db4a2ed7d591442792ba647dcf80<br>                            <br>                        a83d825548a1fb4694e1547579091cb1返回登录5db79b134e9f6b82c0b36e0489ee08ed<br>                    16b28748ea4df4d9c2150843fecfba68<br>                    <br>                f5a47148e367a6035fd7a2faa965022e<br>            16b28748ea4df4d9c2150843fecfba68<br>        16b28748ea4df4d9c2150843fecfba68<br></p>

The key point is to see how to use JQuery to POST data to the background

<p>cf7d6897649c21fc8fd1414f6811503d2cacc6d41bbb37262a98f745aa00fbf0<br>    5cd6e472395e766622bc5d31b556eb7a<br>        $(function(){<br>            $("#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==""){<br>                    alert("所有信息不可为空,请确认!");                    return;<br>                }else if(pwd!=rePwd){<br>                    alert("两次密码输入不一致!");                    return;<br>                }<br>                <br>                $.post("doReg.php",{                    "userName":userName,                    "pwd":pwd,                    "realName":realName<br>                },function(data){<br>                    alert(data);                    <br>                    if(data=="注册成功"){<br>                        location = "login.php";<br>                    }<br>                })<br>                <br>            });<br>        });    2cacc6d41bbb37262a98f745aa00fbf0<br></p>

The php in the background After the file receives the data, it will use the SQL statement to operate the database and store the data in the table

<p>a5d587a141753b69bf46dd48458ee7ee0){        die("用户名已经存在!");  <br>    }    elseif($res){        echo 'true';<br>    }else{        die();<br>    }    <br></p>

In this way, click the registration button , the entered information can be stored in the table. After success, jump to the login page

The next thing is the login page. The login function needs to read the user name and password information stored in the table

The login page style is not much to say. Also remember the required name and ID

<div class="panel panel-primary">
            <div class="panel-heading">
                <div class="panel-title">用户登录</div>
            </div>
            <div class="panel-body">
                <form class="form-horizontal">
                    <div class="form-group">
                        <label>用户名</label>
                        <input type="text" class="form-control" name="userName"/>
                    </div>
                    <div class="form-group">
                        <label>密码</label>
                        <input type="password" class="form-control" name="pwd"/>
                    </div>
                    
                    <div 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>
                    </div>
                    
                </form>
            </div>
        </div>

The focus is still on the JQ code

<p><script src="../../js/jquery-1.10.2.js"></script><br/>    <script type="text/javascript"><br/>        $(function(){<br/>            $("#submit").on("click",function(){                var userName = $("input[name=&#39;userName&#39;]").val();                var pwd = $("input[name=&#39;pwd&#39;]").val();<br/>                <br/>                $.post("doLogin.php",{                    "userName":userName,                    "pwd":pwd<br/>                },function(data){<br/>                    alert(data);                    if(data=="登录成功"){<br/>                        location = "index.php";<br/>                    }else{<br/>                        alert("用户名或密码有误!");<br/>                    }<br/>                });<br/>            });<br/>        });    </script><br/></p>

What the JQ code of the above landing page does is, take Go to the backend to log in to the PHP file. Compare the username and password information read from the database with the new one entered by the user. If true, the login is successful.

So how to write the backend login page? It is very simple. From the SQL statement After reading the information from the table, return to the front desk login page

<p><?php    <br/>header("Content-Type:text/html;charset=utf-8");include_once("../mysql/mysql.php");    <br/>    $userName = $_POST["userName"];    $pwd = $_POST["pwd"];    <br/>    $loginSql = <<<login<br/>    select * from submit where username="{$userName}" and pwd = "{$pwd}";<br/>login;    $res = mysqli_query($con, $loginSql);    <br/>    if($row = mysqli_fetch_row($res)){        $_SESSION["user"] = $row;        echo "登录成功";<br/>    }else{        echo "登录失败";<br/>    }    <br/>    mysqli_free_result($res);    mysqli_close($con);<br/></p>

After successful login, it will prompt that the login is successful and jump to the home page (index.html)

Recommended learning: "PHP video tutorial

The above is the detailed content of How to read query data from mysql database in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn