Ajax+php+mysql simple example of reading the database
Apr 17, 2018 am 09:18 AMThis article introduces a simple example of ajax php mysql reading the database. It has a certain reference value. Now I share it with you. Friends in need can refer to it
1. Create a database
create database ajaxdemo default charset utf8;
use ajaxdemo;
Create a table and insert data
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', '女');
<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"></pre></p> </p> </p> </p> <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("ajaxp"); 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>>
<?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数据格式 ?>
PHP reads the database and sorts it according to the Chinese name implementation code_PHP tutorial
The above is the detailed content of Ajax+php+mysql simple example of reading the database. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

How To Set Up Visual Studio Code (VS Code) for PHP Development
