2024年12月05日 建站教程
如何利用php语法结合mysql语法查询数据库指定日期范围内的数据,下面web建站小编给大家详细介绍一下具体实现代码!
具体代码如下:
<?php //数据库信息 $servername = "localhost"; $username = "username"; $password = "password"; $dbname = "dbname"; // 创建连接 $conn = mysqli_connect($servername, $username, $password, $dbname); // 检查连接 if (!$conn) { die("连接失败: " . mysqli_connect_error()); } $begin_date = '2023-02-01'; $end_date = '2023-02-31'; $sql = "SELECT * FROM table WHERE pub_date BETWEEN '$begin_date' AND '$end_date'"; $result = mysqli_query($conn, $sql); if (mysqli_num_rows($result) > 0) { // 输出数据 while($row = mysqli_fetch_assoc($result)) { echo "序号: " . $row["id"]. " - 金额: " . $row["amount"]. " - 日期: " . $row["pub_date"]. "<br>"; } } else { echo "暂无数据"; } mysqli_close($conn); ?>
本文链接:http://so.lmcjl.com/news/18980/