Ajax+php+mysql simple example of reading the database

不言
Release: 2023-03-24 07:18:01
Original
3446 people have browsed it

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=&#39;userage&#39;/>
							</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=&#39;ajaxFunction()&#39;>提交</button>
					</form>
					
					<table class="table table-condensed table-bordered" id="ajaxp"></table>
					<p>SQL语句:<pre id="sql">

>
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 &#39;UTF8&#39; ");

$userage=$_GET[&#39;userage&#39;];
$usersex=$_GET[&#39;usersex&#39;];

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

$query="select * from ajaxtest where usersex=&#39;$usersex&#39;";

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

if($qry_result->num_rows==0)
{
    echo json_encode([&#39;data&#39;=>&#39;<h2>未找到符合条件的记录</h2>&#39;,&#39;sql&#39;=>$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([&#39;data&#39;=>$display_string,&#39;sql&#39;=>$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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!