Home > Database > Mysql Tutorial > PHP operates the MySQL database to query data and output it to the web page

PHP operates the MySQL database to query data and output it to the web page

coldplay.xixi
Release: 2021-01-11 09:11:20
forward
3672 people have browsed it

PHP operates the MySQL database to query data and output it to the web page

Related free learning recommendations: mysql video tutorial

There is a data table hacker in the database, now we need to query the data in the hacker table And displayed on the page, the code is as follows:

<?php
    // 连接数据库
    $mysqli = mysqli_connect("localhost","root","123",&#39;student&#39;);     //mysqli_close($con);
    $mysqli ? print &#39;yes!<br>&#39;:print "no!<br>";

    $sql = $mysqli -> query(&#39;use student&#39;);
    $sql = $mysqli -> query(&#39;set name utf8&#39;);

    // 创建数据表
    $sql = $mysqli -> query("create table hacker(
        id char(6) not null,
        name char(8) not null,
        chinese float not null,
        math float not null,
        english float not null
    )");
    if($sql == true){
        echo &#39;成功创建hacker表!<br>&#39;;
    }else{
        echo &#39;创建hacker表失败!&#39;.$mysqli->error.&#39;<br>&#39;;
    }

    // 添加数据——hacker
    // $sql  = "insert into hacker values(&#39;1&#39;,&#39;张三&#39;,&#39;10&#39;,&#39;20&#39;,&#39;30&#39;);"; 
    // $sql  .= "insert into hacker values(&#39;2&#39;,&#39;李四&#39;,&#39;66&#39;,&#39;77&#39;,&#39;88&#39;);"; 
    // $sql .= "insert into hacker values(&#39;3&#39;,&#39;王二&#39;,&#39;55&#39;,&#39;44&#39;,&#39;33&#39;);"; 
    // $sql .= "insert into hacker values(&#39;4&#39;,&#39;麻子&#39;,&#39;22&#39;,&#39;55&#39;,&#39;77&#39;)"; 
    // $rows = $mysqli->affected_rows;
    // if($rows == 3){
    //     echo "添加数据成功。<br>";
    // }else{
    //     echo &#39;添加失败。&#39;.$mysqli->error."<br>";
    // }

    $sql = $mysqli -> query("insert into hacker
        values(&#39;2&#39;,&#39;李四&#39;,&#39;66&#39;,&#39;77&#39;,&#39;88&#39;)
    ");
    $rows = $mysqli->affected_rows;
    if($rows == 1){
        echo "添加数据成功。<br>";
    }else{
        echo &#39;添加失败。&#39;.$mysqli->error."<br>";
    }
    $sql = $mysqli -> query("insert into hacker
        values(&#39;3&#39;,&#39;王二&#39;,&#39;55&#39;,&#39;44&#39;,&#39;33&#39;)
    ");
    $rows = $mysqli->affected_rows;
    if($rows == 1){
        echo "添加数据成功。<br>";
    }else{
        echo &#39;添加失败。&#39;.$mysqli->error."<br>";
    }
    $sql = $mysqli -> query("insert into hacker
        values(&#39;4&#39;,&#39;麻子&#39;,&#39;22&#39;,&#39;55&#39;,&#39;77&#39;)
    ");
    $rows = $mysqli->affected_rows;
    if($rows == 1){
        echo "添加数据成功。<br>";
    }else{
        echo &#39;添加失败。&#39;.$mysqli->error."<br>";
    }

    // 数据查询
    $sql = $mysqli->query("select * from hacker");
    if($sql){
        // echo &#39;有数据!<br>&#39;;
        $rows = $sql->num_rows;
        echo &#39;查询结果共有&#39;.$rows.&#39;条记录<br>&#39;;
        echo "<table>
                <tr>
                    <th>序号</th>
                    <th>姓名</th>
                    <th>语文</th>
                    <th>数学</th>
                    <th>英语</th>
                <tr>
        ";

        if($rows){
            while($row = $sql->fetch_assoc()){
                // echo &#39;id = &#39;.$row[&#39;id&#39;].&#39;<br>name = &#39;.$row[&#39;name&#39;].&#39;<br>chinese = &#39;.$row[&#39;chinese&#39;].&#39;<br>math = &#39;.$row[&#39;math&#39;].&#39;<br>english = &#39;.$row[&#39;english&#39;].&#39;<br>&#39;;
                echo "<tr>
                            <td>".$row[&#39;id&#39;]."</td>
                            <td>".$row[&#39;name&#39;]."</td>
                            <td>".$row[&#39;chinese&#39;]."</td>
                            <td>".$row[&#39;math&#39;]."</td>
                            <td>".$row[&#39;english&#39;]."</td>
                    </tr>
                ";
            }
            echo "</table>";
        }else{
            echo &#39;没有您要找的数据。<br>&#39;;
        }
    }else{
        echo &#39;MySQL语句有误。<br>&#39;.$mysqli->error.&#39;<br>&#39;;
    }?>
Copy after login

The above is the detailed content of PHP operates the MySQL database to query data and output it to the web page. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template