Rumah > pangkalan data > tutorial mysql > PHP操作MySQL数据库,实现查询数据并输出到web页面

PHP操作MySQL数据库,实现查询数据并输出到web页面

coldplay.xixi
Lepaskan: 2021-01-11 09:11:20
ke hadapan
3672 orang telah melayarinya

PHP操作MySQL数据库,实现查询数据并输出到web页面

相关免费学习推荐:mysql视频教程

数据库中有数据表hacker,现在我们要将hacker表中的数据查询出来并在页面上显示,代码如下:

<?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;;
    }?>
Salin selepas log masuk

Atas ialah kandungan terperinci PHP操作MySQL数据库,实现查询数据并输出到web页面. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:csdn.net
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel terbaru oleh pengarang
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan