您的位置:首页 > 数据库

MAMP链接数据库并且对数据库操作的简单示例

2017-09-05 09:59 561 查看

链接数据库并且对数据库操作的简单示例

简单的数据库数据查询和输出

<?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<
eada
/span>==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">«</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">»</span>
</a>
</li>
</ul>
</nav>
</div>
</div>
</body>

</html>


`
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息