Ajax+php+mysql Simple example of reading a database

Source: Internet
Author: User
Tags button type
This article is about Ajax+php+mysql read a simple example of the database, has a certain reference value, now share to everyone, the need for friends can refer to


1. Create a database

Create DATABASE Ajaxdemo default CharSet UTF8;



Switch to the current database

Use Ajaxdemo;



Create a table and insert data

CREATE TABLE ' ajaxtest ' (  ' userid ' int (one) not NULL auto_increment COMMENT ' user ID ',  ' username ' varchar () NOT NULL COMMENT ' username ',  ' userpass ' varchar (a) NOT null COMMENT ' password ',  ' userage ' int (one) not null COMMENT ' age ',  ' Userse X ' varchar (1) Not NULL COMMENT ' gender ',  PRIMARY KEY (' userid ')) Engine=innodb DEFAULT Charset=utf8;


Insert INTO ' ajaxtest ' values (' 1 ', ' John Doe ', ' Lisi ', ' 15 ', ' Male '), insert INTO ' ajaxtest ' values (' 2 ', ' Zhang San ', ' Lisi ', ' 20 ', ' female '); Insert INTO ' ajaxtest ' values (' 3 ', ' Harry ', ' Lisi ', ' 25 ', ' Male '), insert INTO ' ajaxtest ' values (' 4 ', ' Han Meimei ', ' Lisi ', ' 25 ', ' Male ') INSERT into ' ajaxtest ' VALUES (' 5 ', ' Zhangli ', ' Lisi ', ' 25 ', ' female ');


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

3. Create ajaxtest.php to respond to index.php requests

<?php error_reporting (0);//Do not display warning message $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 ' = ' 



Results:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.