ajax+php+mysql 读取数据库的简单示例

1.创建数据库
create database ajaxdemo default charset utf8;


    切换到当前数据库
use ajaxdemo;

    
    创建表并插入数据
CREATE TABLE `ajaxtest` (
  `userid` int(11) NOT NULL AUTO_INCREMENT COMMENT '用户id',
  `username` varchar(50) NOT NULL COMMENT '用户名',
  `userpass` varchar(50) NOT NULL COMMENT '密码',
  `userage` int(11) NOT NULL COMMENT '年龄',
  `usersex` varchar(1) NOT NULL COMMENT '性别',
  PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO `ajaxtest` VALUES ('1', '李四', 'lisi', '15', '男');
INSERT INTO `ajaxtest` VALUES ('2', '张三', 'lisi', '20', '女');
INSERT INTO `ajaxtest` VALUES ('3', '王五', 'lisi', '25', '男');
INSERT INTO `ajaxtest` VALUES ('4', '韩梅梅', 'lisi', '25', '男');
INSERT INTO `ajaxtest` VALUES ('5', '张莉', 'lisi', '25', '女');

2.创建index.php,查询数据库中的信息并展示给用户
<html>
	<head>
		<meta charset="utf-8"/>
		<title>ajax实例</title>
		<link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">
	</head>
	<body style="height: 800px;">
		<div class="container">
			<div class="panel panel-default" style="margin-top: 100px;text-align: center;">
				<div class="panel-heading">
					Ajax实例
				</div>
				<div class="panel-body">
					<form class="navbar-form navbar-center" role="search" name="myform">
						<div class="form-group">
							<label>
								年龄:<input type="number" class="form-control" placeholder="Age" name="userage" id='userage'/>
							</label>
						</div>
						<select class="form-control" id="usersex" name="usersex">
							<option value="男">男</option>
							<option value="女">女</option>
						</select>
						<button type="button" class="btn btn-default" onclick='ajaxFunction()'>提交</button>
					</form>
					
					<table class="table table-condensed table-bordered" id="ajaxDiv"></table>
					<p>SQL语句:<pre id="sql"></pre></p>
				</div>
			</div>
		</div>
		
		
		<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
		<script src="https://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
		<script type="text/javascript">
			function ajaxFunction()
			{
				var xmlHttp;
				try{
					xmlHttp = new XMLHttpRequest();
				}catch(e){
					//IE浏览器需要用ActiveXObject来创建 XMLHttpRequest对象
					try{
						 //如果Javascript的版本大于5
						xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
					}catch(e){
						try{
							 //如果不是 则使用老版本的ActiveX对象
							xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
						}catch(e){
							alert("您的浏览器不支持");
							return false;
						}
					}
				}
				xmlHttp.onreadystatechange=function(){
					if(xmlHttp.readyState==4){
						var ajaxData=document.getElementById("ajaxDiv");
						var sqlData=document.getElementById('sql');
						var jsonData=JSON.parse(xmlHttp.responseText);//解析json数据
						ajaxData.innerHTML=jsonData.data;
						sqlData.innerHTML=jsonData.sql;
					}
				}
				var userage=document.getElementById('userage').value;
				var usersex=document.getElementById('usersex').value;
				var url='?userage='+userage;
				url += '&usersex='+usersex;
				xmlHttp.open("GET","ajaxTest.php"+url,true);
				xmlHttp.send(); 
			}
		</script>
	</body>
</html>>
3.创建ajaxtest.php来响应index.php的请求
<?php
    error_reporting(0);//不显示警告信息
    $dbhost="localhost";
    $dbuser="root";
    $dbpass="root";
    $dbname="ajaxdemo";

$mysqli=new mysqli($dbhost,$dbuser,$dbpass,$dbname);
$mysqli->query("SET NAMES 'UTF8' ");

$userage=$_GET['userage'];
$usersex=$_GET['usersex'];

$userage=$mysqli->real_escape_string($userage);
$usersex=$mysqli->real_escape_string($usersex);

$query="select * from ajaxtest where usersex='$usersex'";

if(is_numeric($userage))
{
    $query .="AND userage <= $userage;";
}
$qry_result=$mysqli->query($query);

if($qry_result->num_rows==0)
{
    echo json_encode(['data'=>'<h2>未找到符合条件的记录</h2>','sql'=>$query]);
    return ;
}

$display_string ="<tr>";
$display_string .="<td>用户名</td>";
$display_string .="<td>年龄</td>";
$display_string .="<td>性别</td>";
$display_string .="</tr>";

//insert a new row in the table for each person returned
while($row=mysqli_fetch_object($qry_result)){
    $display_string.="<tr>";
    $display_string.="<td>$row->username</td>";
    $display_string.="<td>$row->userage</td>";
    $display_string.="<td>$row->usersex</td>";
    $display_string.="</tr>";
}

echo json_encode(['data'=>$display_string,'sql'=>$query]);//返回json数据格式
?>


结果:



阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页