MAMP鏈接數據庫並且對數據庫操作的簡單示例

鏈接數據庫並且對數據庫操作的簡單示例

  • 簡單的數據庫數據查詢和輸出
<?php
    //簡單的數據庫數據查詢和輸出
    $db = mysqli_connect("127.0.0.1","root","root","student_content");
    //鏈接數據庫,MAMP的默認地址是127.0.0.1,默認的用戶名是root密碼是root,數據表的名字是student_content
    $sql = "select * from student";
    //數據庫的SQL命令:從student查詢抓取數據
    $result = mysqli_query($db,$sql);
    //從鏈接的數據庫裏發送指令,查詢數據庫信息;
    $list = mysqli_fetch_all($result,MYSQLI_ASSOC);  
    //從數據庫抓取數據,數據以列命名命名獲取
?>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8" />
    <title>Document</title>
    <link href="https://cdn.bootcss.com/bootstrap/4.0.0-alpha/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <table class="table">
        <tr>
            <th>序號</th>
            <th>學號</th>
            <th>姓名</th>
            <th>單片機</th>
            <th>數學</th>
            <th>英語</th>
            <th>C語言</th>
            <th>總成績</th>
        </tr>
        <?php
            foreach($list as $v){
        ?>
            <tr>
                <td><?php echo $v['id'];?></td>
                <td><?php echo $v['num'];?></td>
                <td><?php echo $v['name'];?></td>
                <td><?php echo $v['dpj_results'];?></td>
                <td><?php echo $v['math_results'];?></td>
                <td><?php echo $v['English_results'];?></td>
                <td><?php echo $v['c_results'];?></td>
                <td><?php echo $v['all_results'];?></td>
            </tr>
        <?php
        }
        ?>
    </table>
</body>
</html>


// php數據庫的操作,
<?php
    $link = mysqli_connect('127.0.0.1','root','root','student_content');
    if(empty($_GET['page'])){
        $page=1;
    }else{
        $page=$_GET['page'];
    }
    $size=2;

    $n= mysqli_query($link,"select count(*) as n from student"); 
    $n=mysqli_fetch_assoc($n);    
    $n=$n['n'];    
    $all_page=ceil($n/$size);
    $start = ($page-1)*$size;
    $page_before=$page==1?$page:$page-1;
    $page_next=$page==$all_page?$page:$page+1;
    $sql = "select * from student limit $start,$size ";
    $result = mysqli_query($link,$sql);
    // $chang = mysql_num_rows($result);
    //查詢這個獲取到的$result的長度
    $list = mysqli_fetch_all($result,MYSQLI_ASSOC);
    $arr = array("男","女");
?>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
            <meta name="viewport" content="width=device-width, initial-scale=1,user-scalable=no">
        <title></title>
        <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
        <script src="cjk/jquery.js"></script>
        <script src="cjk/bootstrap.js"></script>
        <script src="cjk/flexible.js"></script>
        <style>
            .right {
                width: 80%;
                display: inline-block;
                box-sizing: border-box;
                margin-top:10px ;
            }
            .container{
                width: 100%;
                margin-top: 15px;
                text-align: center;
            }
            .h3{
                margin-top:20px ;
                text-align: center;
                margin-top: 80px ;
            }

    .breadcrumb{
        font-size: 18px;
        padding: 20px 40px;
    }
    th{
        text-align: center;
    }
        </style>
    </head>
    <body>      
        <ol class="breadcrumb">
            <li class="active">Home</li>
            <li><a href="content_home.php">學生管理</a></li>
            <li><a href="content_inset.html">增加學生</a></li>
        </ol>
        <h3 class="h3">學員信息</h3>
        <div class="container">
            <div class="right">
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>排序</th>
                            <th>學號</th>
                            <th>姓名</th>
                            <th>年齡</th>
                            <th>性別</th>
                            <th>單片機</th>
                            <th>數學</th>
                            <th>英語</th>
                            <th>C語言</th>
                            <th>總成績</th>
                        </tr>
                    </thead>
                    <tbody>
                    <?php
                        foreach($list as $k=>$v){
                    ?>

                        <tr>
                            <td><?php echo $v['id']; ?></td>
                            <td><?php echo $v['num']; ?></td>
                                <td><?php echo $v['name']; ?></td>
                                <td><?php echo $v['sex']; ?></td>
                                <td><?php echo $arr[$v['age']-1]; ?></td>                   
                            <td><?php echo $v['dpj_results']; ?></td>
                            <td><?php echo $v['math_results']; ?></td>
                            <td><?php echo $v['English_results']; ?></td>
                            <td><?php echo $v['c_results']; ?></td>
                            <td><?php echo $v['dpj_results']+$v['math_results']+$v['English_results']+$v['c_results']; ?></td>
                      </tr>

                    <?php
                    }
                    ?>
                    </tbody>
                </table>



                <nav aria-label="Page navigation">
                    <ul class="pagination">
                        <li>
                            <a href="http://localhost/frameset/content_fist.php?page=<?php echo $page_before; ?>" aria-label="Previous">
                                <span aria-hidden="true">&laquo;</span>
                            </a>
                        </li>
                        <?php for($i=1;$i<=$all_page;$i++){ ?>
                        <li <?php echo $page==$i?'class="active"':''; ?>>
                            <a href="http://localhost/frameset/content_fist.php?page=<?php echo $i; ?>"><?php echo $i; ?></a>
                        </li>

                        <?php } ?>
                        <li>
                            <a href="http://localhost/frameset/content_fist.php?page=<?php echo $page_next; ?>" aria-label="Next">
                                <span aria-hidden="true">&raquo;</span>
                            </a>
                        </li>
                    </ul>
                </nav>
            </div>
        </div>
    </body>

    </html>

`

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章