In web development, it is very common to use Ajax and PHP technology to query the database. Through Ajax, you can use PHP code to query the database without refreshing the entire page, thereby achieving a more efficient dynamic page interaction effect. Before querying the database with Ajax and PHP, it is important to understand the data types. This article will introduce how to query database data types through Ajax and PHP.
1. Query string type data
The string type is one of the most common data types in the database. The following takes the user name query of the user table as an example to show how to use Ajax and PHP to query string type data.
HTML part:
<script></script> <script> $(document).ready(function(){ $("#btn").click(function(){ var username=$("#username").val(); $.ajax({ url: "query.php", type: "post", data:{username:username}, success:function(data){ $("#result").html(data); } }); }); }); </script> <input> <input> <div></div>
In the HTML code, enter the user name you want to query in the input box, click the query button, and send the entered user name to the query.php file through Ajax Perform a query, and the query results are returned through the success function and displayed on the page.
PHP part:
<?php $conn=mysqli_connect("localhost","root","password","test_database"); mysqli_query($conn,"set names utf8"); $username=$_POST['username']; $sql="select * from user where username like '%".$username."%'"; $result=mysqli_query($conn,$sql); if(mysqli_num_rows($result)){ while($row=mysqli_fetch_array($result)){ echo "用户名:".$row['username']."<br/>"; } }else{ echo "该用户不存在"; } } mysqli_close($conn); ?>
In the PHP code, first connect to the database, then receive the query keyword sent through $_POST, and then store the query results in the $result array. And retrieve the data one by one through the mysqli_fetch_array function.
2. Query numeric type data
Numeric type data is usually used to store numerical types, such as integers, floating point, etc. The following takes querying the price of a product table as an example to show how to use Ajax and PHP to query numeric data.
HTML part:
<script></script> <script> $(document).ready(function(){ $("#btn").click(function(){ var price=$("#price").val(); $.ajax({ url: "query.php", type: "post", data:{price:price}, success:function(data){ $("#result").html(data); } }); }); }); </script> <input> <input> <div></div>
In the HTML code, enter the price of the product you want to query in the input box, click the query button, and send the entered price to the query.php file through Ajax. Query, the query results are returned through the success function and displayed on the page.
PHP part:
<?php $conn=mysqli_connect("localhost","root","password","test_database"); mysqli_query($conn,"set names utf8"); $price=$_POST['price']; $sql="select * from goods where price='".$price."'"; $result=mysqli_query($conn,$sql); if(mysqli_num_rows($result)){ while($row=mysqli_fetch_array($result)){ echo "商品名称:".$row['name']."<br/>"; } }else{ echo "该价格对应的商品不存在"; } } mysqli_close($conn); ?>
In the PHP code, first connect to the database, then receive the query keyword sent through $_POST, and then store the query results in the $result array. And retrieve the data one by one through the mysqli_fetch_array function.
3. Query date type data
The date type is another common data type in the database. The following takes querying the order time of the order table as an example to show how to use Ajax and PHP to query date type data.
HTML part:
<script></script> <script> $(document).ready(function(){ $("#btn").click(function(){ var date=$("#date").val(); $.ajax({ url: "query.php", type: "post", data:{date:date}, success:function(data){ $("#result").html(data); } }); }); }); </script> <input> <input> <div></div>
In the HTML code, enter the order date you want to query in the input box, click the query button, and send the entered date to the query.php file through Ajax Query is performed in the query, and the query results are returned through the success function and displayed on the page.
PHP part:
<?php $conn=mysqli_connect("localhost","root","password","test_database"); mysqli_query($conn,"set names utf8"); $date=$_POST['date']; $sql="select * from order where order_date='".$date."'"; $result=mysqli_query($conn,$sql); if(mysqli_num_rows($result)){ while($row=mysqli_fetch_array($result)){ echo "订单号:".$row['order_id']."<br/>"; } }else{ echo "该日期没有订单"; } } mysqli_close($conn); ?>
In the PHP code, first connect to the database, then receive the query keyword sent through $_POST, and then store the query results in the $result array. And retrieve the data one by one through the mysqli_fetch_array function.
To sum up, this article introduces how to use Ajax and PHP technology to query database data types from three aspects: strings, numbers and dates. For web developers, mastering this technology is very necessary. I hope this article can be helpful to readers.
The above is the detailed content of How to query database data types via Ajax and PHP. For more information, please follow other related articles on the PHP Chinese website!

This article explores efficient PHP array deduplication. It compares built-in functions like array_unique() with custom hashmap approaches, highlighting performance trade-offs based on array size and data type. The optimal method depends on profili

This article explores PHP array deduplication using key uniqueness. While not a direct duplicate removal method, leveraging key uniqueness allows for creating a new array with unique values by mapping values to keys, overwriting duplicates. This ap

This article analyzes PHP array deduplication, highlighting performance bottlenecks of naive approaches (O(n²)). It explores efficient alternatives using array_unique() with custom functions, SplObjectStorage, and HashSet implementations, achieving

This article details implementing message queues in PHP using RabbitMQ and Redis. It compares their architectures (AMQP vs. in-memory), features, and reliability mechanisms (confirmations, transactions, persistence). Best practices for design, error

This article examines current PHP coding standards and best practices, focusing on PSR recommendations (PSR-1, PSR-2, PSR-4, PSR-12). It emphasizes improving code readability and maintainability through consistent styling, meaningful naming, and eff

This article details installing and troubleshooting PHP extensions, focusing on PECL. It covers installation steps (finding, downloading/compiling, enabling, restarting the server), troubleshooting techniques (checking logs, verifying installation,

This article explores optimizing PHP array deduplication for large datasets. It examines techniques like array_unique(), array_flip(), SplObjectStorage, and pre-sorting, comparing their efficiency. For massive datasets, it suggests chunking, datab

This article explains PHP's Reflection API, enabling runtime inspection and manipulation of classes, methods, and properties. It details common use cases (documentation generation, ORMs, dependency injection) and cautions against performance overhea


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
