Home Backend Development PHP Tutorial Ajax+php+mysql simple example of reading the database

Ajax+php+mysql simple example of reading the database

Apr 17, 2018 am 09:18 AM
php database

This 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;
Copy after login



## Switch to the current database

use ajaxdemo;
Copy after login


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;
Copy after login


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', '女');
Copy after login


2. Create index.php, query the information in the database and display it to the user

<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>>
Copy after login

3. Create ajaxtest.php to respond to the request of 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数据格式
?>
Copy after login



Result:


Related recommendations:

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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

CakePHP Date and Time

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

CakePHP File upload

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

CakePHP Routing

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

Discuss CakePHP

CakePHP Quick Guide CakePHP Quick Guide Sep 10, 2024 pm 05:27 PM

CakePHP Quick Guide

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

CakePHP Project Configuration

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

See all articles