这篇文章介绍的内容是关于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;"> <p class="container"> <p class="panel panel-default" style="margin-top: 100px;text-align: center;"> <p class="panel-heading"> Ajax实例 </p> <p class="panel-body"> <form class="navbar-form navbar-center" role="search" name="myform"> <p class="form-group"> <label> 年龄:<input type="number" class="form-control" placeholder="Age" name="userage" id='userage'/> </label> </p> <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="ajaxp"></table> <p>SQL语句:<pre id="sql">