How to query the data of a certain year in the database in php

PHPz
Release: 2023-03-23 17:32:01
Original
1390 people have browsed it

PHP is a programming language that can execute server-side scripts. It is widely used in developing Web applications. The most critical one is to query data from the database to display on the page. How to query data for a certain year in PHP? Below, we will introduce it to you in detail.

First, we need to connect to the database, for example, to connect to the MySql database, you can use the following code:

// 连接数据库
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
}
Copy after login

After connecting to the database, we need to prepare SQL statements to query the data. The following is the SQL statement to query a specific year:

SELECT * FROM tableName WHERE YEAR(dateColumn) = 'yearValue';
Copy after login

In the above code, we extract the year from the dateColumn through the YEAR function and compare it with yearValue to query the data for a specific year. Please replace tableName and dateColumn with the correct table name and date column name.

If you want to query the data of a certain month, you can use the following code:

SELECT * FROM tableName WHERE YEAR(dateColumn) = 'yearValue' AND MONTH(dateColumn) = 'monthValue';
Copy after login

Here, we extract the month from the dateColumn through the MONTH function and compare it with the monthValue to query Data for a specific year and month.

Finally, please remember to close the database connection. The following is an example of the complete code:

// 连接数据库
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

// 检查连接是否成功
if ($conn->connect_error) {
    die("连接失败:" . $conn->connect_error);
}

// 查询特定年份的数据
$year = 2021;
$sql = "SELECT * FROM tableName WHERE YEAR(dateColumn) = '$year'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // 输出数据
    while($row = $result->fetch_assoc()) {
        echo "id: " . $row["id"]. " - Name: " . $row["name"]. " - Date: " . $row["dateColumn"]. "
";     } } else {     echo "0 结果"; } // 关闭数据库连接 $conn->close();
Copy after login

With the above code, we can easily query the data of the specified year or month. Hope this article helps you!

The above is the detailed content of How to query the data of a certain year in the database in php. 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!