Home  >  Article  >  Backend Development  >  How to query database data types via Ajax and PHP

How to query database data types via Ajax and PHP

PHPz
PHPzOriginal
2023-03-29 11:32:37490browse

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:

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></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>
</head>
<body>
<input type="text" id="username" name="username">
<input type="button" id="btn" value="查询">
<div id="result"></div>
</body>
</html>

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[&#39;username&#39;];
$sql="select * from user where username like &#39;%".$username."%&#39;";

$result=mysqli_query($conn,$sql);

if(mysqli_num_rows($result)){
  while($row=mysqli_fetch_array($result)){
    echo "用户名:".$row[&#39;username&#39;]."<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:

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></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>
</head>
<body>
<input type="text" id="price" name="price">
<input type="button" id="btn" value="查询">
<div id="result"></div>
</body>
</html>

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[&#39;price&#39;];
$sql="select * from goods where price=&#39;".$price."&#39;";

$result=mysqli_query($conn,$sql);

if(mysqli_num_rows($result)){
  while($row=mysqli_fetch_array($result)){
    echo "商品名称:".$row[&#39;name&#39;]."<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:

<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></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>
</head>
<body>
<input type="text" id="date" name="date">
<input type="button" id="btn" value="查询">
<div id="result"></div>
</body>
</html>

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[&#39;date&#39;];
$sql="select * from order where order_date=&#39;".$date."&#39;";

$result=mysqli_query($conn,$sql);

if(mysqli_num_rows($result)){
  while($row=mysqli_fetch_array($result)){
    echo "订单号:".$row[&#39;order_id&#39;]."<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!

Statement:
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