一、配置環境
1、配置好SqlServer的登錄名:sc。參考:https://jingyan.baidu.com/article/8cdccae9452b3c315513cd52.html
2、下載 php_pdo_sqlsrv_74_ts_x64.dll 和 php_sqlsrv_74_ts_x64.dll兩個文件並放到 php 的ext目錄下。下載地址:https://www.zhaodll.com/dll/p/201905/343232.html
3、在你的php.ini文件中加上
extension=php_pdo_sqlsrv_74_ts_x64.dll extension=php_sqlsrv_74_ts_x64.dll
4、重啟Apache服務器。
5、測試:
$serverName = "localhost"; //數據庫服務器地址
$uid = "sa"; //數據庫用戶名
$pwd = "lxy208751"; //數據庫密碼
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database" => "Students"); $conn = sqlsrv_connect($serverName, $connectionInfo); if ($conn == false) { echo "連接失敗!"; var_dump(sqlsrv_errors()); exit; } else { echo "鏈接成功"; }

二、編碼設置
(注:由於sql server 不支持UTF8,當使用varchar保存漢字時,會出現亂碼。需要轉換。)
1、從數據庫查出來的數據編碼“GBK”,一般我們有的是“UTF-8”,所以需要轉換成“UTF-8”。
function toU8($str){ return iconv('GBK', 'UTF-8', $str); }
2、從程序代碼中的變量插入的數據庫時,需要將變量的編碼改成“GBK”才能插入數據庫。
function toGBK($str){ return iconv('UTF-8', 'GBK', $str); }
三、學生信息管理系統
1、index.php
<?php include('./conn/conn.php'); $sql="select * from student"; $result = sqlsrv_query( $conn, $sql ) or die("數據查詢失敗!"); if(!empty($_GET)){ $delete=$_GET['delete']; if($delete='1'){ echo "<script>alert('刪除成功!');</script>"; }else{ echo "<script>alert('刪除失敗!');</script>"; } } function toU8($str){ return iconv('GBK', 'UTF-8', $str); } ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>學生管理系統</title>
<link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" />
<script src="js/jquery-1.11.3.min.js" type="text/javascript"></script>
<script src="js/bootstrap.min.js" type="text/javascript"></script>
<style type="text/css">
/*body { background-repeat: no-repeat; background-size:cover; background-attachment: fixed; background-image: url(image/c_b2.jpg); background-position: 0px -80px; }*/ table { margin: auto; width: 80%; text-align: center; background-color: #ffffff;
} h1 { font-family: 華文行楷; } </style>
<script type="text/javascript">
function deleteStudent(sno) { if (confirm("您是否要刪除該項?")) { location.href = "delete.php?sno="+sno; } } </script>
</head>
<body>
<div>
<h1 align="center">學生信息管理系統</h1>
<h3 align="center">
<a href="add.php">添加學生信息</a>
</h3>
</div>
<div style="align-content: center;">
<table class="table table-bordered" style="width: 80%;">
<thead>
<tr>
<th><p align="center">學號</p></th>
<th><p align="center">姓名</p></th>
<th><p align="center">性別</p></th>
<th><p align="center">年齡</p></th>
<th><p align="center">學院</p></th>
<th colspan="2"><p align="center">操作</p></th>
</tr>
</thead>
<tbody>
<?php while($row = sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC)){ ?>
<tr>
<td><?php echo toU8($row['Sno']);?></td>
<td><?php echo toU8($row['Sname']);?></td>
<td><?php echo toU8($row['Ssex']);?></td>
<td><?php echo toU8($row['Sage']);?></td>
<td><?php echo toU8($row['Sdept']);?></td>
<td><a href="update.php?sno=<?php echo toU8($row['Sno']);?>">修改</a></td>
<td><a href="javascript:;" onclick="deleteStudent(<?php echo toU8($row['Sno']);?>)">刪除</a></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<div>
<hr>
<h1 align="center" style="font-size: 20px;">石家庄鐵道大學 @2018-2020</h1>
</div>
</body>
</html>
2、add.php
<?php include('./conn/conn.php'); function toGBK($str){ return iconv('UTF-8', 'GBK', $str); } if(!empty($_POST)){ $name=$_POST['name']; $sex=$_POST['sex']; $sno=$_POST['sno']; $dept=$_POST['dept']; $age=$_POST['age']; $sno=toGBK($sno); $name=toGBK($name); $sex=toGBK($sex); $dept=toGBK($dept); $sql = "insert into Student(Sno,Sname,Ssex,Sage,Sdept) values('$sno','$name','$sex',$age,'$dept')"; echo $sql; $result = sqlsrv_query($conn, $sql) or die("數據插入失敗!"); header("location:index.php"); } ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加學生信息</title>
<style type="text/css"> html { font-size: 15px; } fieldset { width: 450px; margin: 0 auto; } legend { font-weight: bold; font-size: 18px; } label { float: left; width: 70px; margin-left: 10px; } .left { margin-left: 80px; } .input { width: 200px; border: 1px solid #ccc;
padding: 7px 0px; border-radius: 3px; /*css3屬性IE不支持*/ padding-left: 5px; } span { color: #666666;
} p{ margin-left: 30px; } </style>
</head>
<body>
<fieldset>
<legend>添加個人信息</legend>
<form name="addForm" method="post" action="#">
<p>
<label for="sno" class="label">學號:</label>
<input id="sno" name="sno" type="text" class="input" />
<p/>
<p>
<label for="name" class="label">姓名:</label>
<input id="name" name="name" type="text" class="input" />
<p/>
<p>
<label for="sex" class="label">性別:</label>
<input name="sex" type="radio" value="男" >男 <input name="sex" type="radio"value="女">女 <p/>
<p>
<label for="age" class="label">年齡:</label>
<input id="age" name="age" type="text" class="input" />
<p/>
<p>
<label for="dept" class="label">學院:</label>
<input id="dept" name="dept" type="text" class="input" />
<p/>
<p>
<input type="reset" value=" 重 置 "/>
<input type="submit" name="submit" value=" 提 交 " class="left" />
</p>
</form>
</fieldset>
</body>
</html>
3、undate.php
<?php include('./conn/conn.php'); function toU8($str){ return iconv('GBK', 'UTF-8', $str); } function toGBK($str){ return iconv('UTF-8', 'GBK', $str); } if(!empty($_GET) ){ $sno=$_GET['sno']; $sno=toGBK($sno); $sql="select * from student where Sno='$sno'"; $result = sqlsrv_query($conn, $sql) or die("數據查詢失敗!"); $row=sqlsrv_fetch_array( $result, SQLSRV_FETCH_ASSOC); } ?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>修改學生信息</title>
<style type="text/css"> html { font-size: 15px; } fieldset { width: 450px; margin: 0 auto; } legend { font-weight: bold; font-size: 18px; } label { float: left; width: 70px; margin-left: 10px; } .left { margin-left: 80px; } .input { width: 200px; border: 1px solid #ccc;
padding: 7px 0px; border-radius: 3px; /*css3屬性IE不支持*/ padding-left: 5px; } span { color: #666666;
} p{ margin-left: 30px; } </style>
</head>
<body>
<fieldset>
<legend>修改學生信息</legend>
<form name="addForm" method="post" action="change.php">
<p>
<label for="sno" class="label">學號:</label>
<input id="sno" name="sno" type="text" class="input" value="<?php echo toU8($row['Sno']); ?>" />
<p/>
<p>
<label for="name" class="label">真實姓名:</label>
<input id="name" name="name" type="text" class="input" value="<?php echo toU8($row['Sname']); ?>"/>
<p/>
<p>
<label for="sex" class="label">性別:</label>
<input name="sex" type="radio" value="男"
<?php if(toU8($row['Ssex'])=='男'){ echo "checked='checked'"; } ?>
>男 <input name="sex" type="radio" value="女"
<?php if(toU8($row['Ssex'])=='女'){ echo "checked='checked'"; } ?>
>女 <p/>
<p>
<label for="age" class="label">年齡:</label>
<input id="age" name="age" type="text" class="input" value="<?php echo toU8($row['Sage']); ?>"/>
<p/>
<p>
<label for="dept" class="label">學院:</label>
<input id="dept" name="dept" type="text" class="input" value="<?php echo toU8($row['Sdept']); ?>"/>
<p/>
<p>
<input type="reset" value="重置"/>
<input type="submit" name="submit" value=" 確 定 " class="left" />
</p>
</form>
</fieldset>
</body>
</html>
4、change.php
<?php include('./conn/conn.php'); function toGBK($str){ return iconv('UTF-8', 'GBK', $str); } if(!empty($_POST)){ $name=$_POST['name']; $sex=$_POST['sex']; $sno=$_POST['sno']; $dept=$_POST['dept']; $age=$_POST['age']; $sno=toGBK($sno); $name=toGBK($name); $sex=toGBK($sex); $dept=toGBK($dept); $sql = "update Student set Sno ='$sno', Sname='$name', Ssex='$sex',Sage=$age, Sdept='$dept' where Sno ='$sno'"; echo $sql; $result = sqlsrv_query($conn, $sql) or die("數據更新失敗!"); header("location:index.php"); }
5、delete.php
<?php include('./conn/conn.php'); function toGBK($str){ return iconv('UTF-8', 'GBK', $str); } if(!empty($_GET)){ $sno=$_GET['sno']; $sno=toGBK($sno); $sql_d="delete from student where Sno='$sno'"; $result_d = sqlsrv_query($conn, $sql_d) or die("數據查詢失敗!"); if($result_d){ header("location:index.php?delete=1"); }else{ header("location:index.php?delete=0"); } }
6、conn.php
<?php header("content-Type: text/html; charset=utf-8"); $serverName = "localhost"; //數據庫服務器地址
$uid = "sa"; //數據庫用戶名
$pwd = "lxy208751"; //數據庫密碼
$connectionInfo = array("UID" => $uid, "PWD" => $pwd, "Database" => "Students"); $conn = sqlsrv_connect($serverName, $connectionInfo);
四、視圖







