使用AngularJS实现的本地数据库(xampp)访问

第一部分连接数据库
<?php
//连接数据库
$dbHost="localhost";
$dbUserName="root";
$dbPWD="123456";
$dbName="angular";
$char_code="utf-8";

$conn=new mysqli($dbHost,$dbUserName,$dbPWD,$dbName);

第二部分数据库查询

<?php
require_once('SQL.php');
header("Conntent-type:text/json");
$sex=$_GET["sex"];
//0:女。1:男。2:全部
$conn->query("set names utf8");
if($sex=='女'||$sex=='男'){
    $result=$conn->query("select * from student where sex='$sex'");
}else{
    $result=$conn->query("select * from student");
}
$arr=[];
if($result){
        foreach($result as $thisVal){
            array_push($arr,$thisVal);
        }
        echo json_encode(array("data"=>$arr));
    }

第三部分

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
		<script type="text/javascript" src="../js/angular-1.3.0.js"></script>
		<link rel="stylesheet" type="text/css" href="../css/bootstrap.css" />
	</head>

	<body style="min-width: 150px;" ng-app="myApp" ng-controller="myController">
		<button class="btn btn-danger" ng-click="clickAction($event)" id="男">男生信息</button>
		<button class="btn btn-warning" ng-click="clickAction($event)" id="女">女生信息</button>
		<button class="btn btn-info" ng-click="clickAction($event)" id="">全部</button>
		<table class="table table-striped">
			<tr class="warning">
				<td>学号</td>
				<td>姓名</td>
				<td>性别</td>
				<td>年龄</td>
			</tr>
			<tr class="active" ng-repeat="student in aa">
				<td>{{student.ID}}</td>
				<td>{{student.name}}</td>
				<td>{{student.sex}}</td>
				<td>{{student.age}}</td>
			</tr>
		</table>
			<script type="text/javascript">
				var myApp=angular.module("myApp",[]);
				myApp.controller("myController",function($scope,$http){
					$scope.clickAction=function(event){
					$scope.aa=[];

						var Se=event.target.id;

						console.log(Se);
						$http.get("http://192.168.10.151/newANGULAR/SQL_ITEM/Angular_SQL.php?sex="+Se).success(function(res){
//							$scope.aa=[];
							for(var i = 0; i < res.data.length; i++) {
								$scope.aa.push(res.data[i]);
							}
							console.log(res);

							console.log(res.data.length);
						});
					}
				});
				
				
			</script>
	</body>

</html>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值