Select MySQLi Data By Monthly using PHP

By CampCodes Administrator

Updated on:

select mysqli data by monthly using php

In this tutorial, we will create a Select MySQLi Data By Monthly using PHP. This code will select a specific MySQLi table row when the user submits a month and year from a form. The code use MySQLi SELECT query to select data in the MySQLi row, and by providing a month and year in the WHERE clause, it will display that particular database on the given month and year. This a user-friendly program; feel free to modify and use it to your system.

We will be using PHP as a scripting language and interpreter that is used primarily on any web server, including xamp, wamp, etc. It is being used to any popular websites because of the modern approach as its today.

Getting Started:

First, you have to download & install XAMPP or any local server that runs PHP scripts. Here’s the link for the XAMPP server https://www.apachefriends.org/index.html.

And, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_month, after that, click Import, then locate the database file inside the folder of the application then click ok.

2019 10 30 23 20 23 localhost 127.0.0.1 db detail phpmyadmin 4.8.3

Creating the database connection

Open your any kind of text editor(notepad++, etc..). Then just copy/paste the code below then name it conn.php.

<?php
    $conn=mysqli_connect("localhost", "root", "", "db_month");
 
    if(!$conn){
        die("Error: Failed to connect to database!");
    }
?>

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as index.php.

READ ALSO:   Basic Array Functions in PHP

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
        <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
    </head>
<body>
    <nav class="navbar navbar-default">
        <div class="container-fluid">
            <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
        </div>
    </nav>
    <div class="col-md-3"></div>
    <div class="col-md-6 well">
        <h3 class="text-primary">PHP - Select MySQLi Data By Monthly</h3>
        <hr style="border-top:1px dotted #ccc;"/>
        <div class="col-md-2"></div>
        <div class="col-md-8">
            <form method="POST" action="">
                <div class="form-inline">
                    <label>Month: </label>
                    <select name="month" class="form-control">
                        <?php
                            for ($i = 1; $i <= 12; $i++){
                                $month = date('F', mktime(0, 0, 0, $i, 1, 2019));
                        ?>
                                <option value="<?php echo $i; ?>"><?php echo $month; ?></option>
                        <?php
                            }
                        ?>
                    </select>
                    <label>Year: </label>
                    <select name="year" class="form-control">
                        <?php
                            for ($i = date('Y'); $i >= 1965; $i--){
                        ?>
                            <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
                        <?php
                            }
                        ?>
                    </select>
                    <button class="btn btn-primary" name="search">Search</button>
                </div>
            </form>
            <h4>Selected: <span class="text-primary"><?php if(ISSET($_POST['search'])){echo date("F", mktime(null, null, null, $_POST['month']));}?></span> <span class="text-primary"><?php if(ISSET($_POST['search'])){echo $_POST['year'];}?></span></h4>
            <table class="table table-bordered">
                <thead class="alert-info">
                    <tr>
                        <th>Firstname</th>
                        <th>Lastname</th>
                        <th>Data Joined</th>
                    </tr>	
                </thead>
                <tbody>
                    <?php include'search.php'?>
                </tbody>
            </table>
        </div>
    </div>
</body>	
</html>

Creating the Main Function

This code contains the main function of the application. This code will select a specific MySQLi table row when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as search.php

<?php
    require'conn.php';
 
    if(ISSET($_POST['search'])){
        $month=$_POST['month'];
        $year=$_POST['year'];
        $query=mysqli_query($conn, "SELECT * FROM `member` WHERE month(date_joined)='$month' AND year(date_joined)='$year'") or die(mysqli_error());
        while($fetch=mysqli_fetch_array($query)){
            echo "<tr><td>".$fetch['firstname']."</td><td>".$fetch['lastname']."</td><td>".$fetch['date_joined']."</td></tr>";
        }
    }else{
        $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
        while($fetch=mysqli_fetch_array($query)){
            echo "<tr><td>".$fetch['firstname']."</td><td>".$fetch['lastname']."</td><td>".$fetch['date_joined']."</td></tr>";
        }
    }
?>
compare month and year in mysql date between in mysql query with example date between two dates in mysql filter data by date in php filter range of date using between query in php mysql get all dates of month in mysql get current month in mysql get last 15 days records in mysql get month number from month name in mysql get the month in mysql how to check date between two dates in mysql how to fetch data date wise from database in php how to get current month in codeigniter how to get current month in mysql query how to get current week data in mysql how to get last 3 months data in mysql how to get last one month data in mysql how to get last one week data in mysql how to get last week data in mysql how to get month and year from date in mysql how to get month wise data in mysql how to get month wise data in mysql php how to get monthly data in sql server how to get number of days in a month in mysql how to insert current date in mysql using php how to search the records between two dates using php codeigniter month mysql mysql between date mysql between time mysql between two dates example mysql current month mysql date between two dates mysql date between two dates example mysql date format mysql date functions mysql date range query mysql date to string mysql date_format mysql datetime mysql extract date from datetime mysql generate list of dates mysql get current month records from datetime mysql get current month records from timestamp mysql get month from timestamp mysql get month name from date mysql group by month mysql last 30 days timestamp mysql last month mysql last year mysql month mysql month function mysql month name mysql query between two dates mysql query date range between mysql query to get last 6 months data mysql select 12 months mysql select current month mysql select time between two dates mysql select where month and year php mysql between date range php tutorials select data between two dates in codeigniter select data between two dates in mysql select data between two dates in mysql php select mysql based only on month and year select mysqli data by monthly using php select where last month sql query to get data between two dates sql query to get data between two months sql select by month and year sql where month equals start date and end date query in mysql

Leave a Comment