Table of Contents
Tutorial: How to Select Data By Month and Year in PHP MySQL with Source Code
Getting Started on How to Select Data By Month and Year in PHP MySQL
This tutorial will show you How to Select Data By Month and Year in PHP/MySQL. In my previous tutorial, I’ve discussed How to Select Data between two Dates. So, I’ve created another topic regarding selecting data with dates which are on How to Select Data by Month and Year using PHP. Also, in this tutorial, I’ve created two MySQL methods that I’ve added in the comment, so feel free to switch between them.
Creating our Database
First, we’re going to create a database that contains our data.
1. Open phpMyAdmin.
2. Click databases, create a database and name it as “month_year”.
3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
CREATE TABLE `login` ( `logid` INT(11) NOT NULL AUTO_INCREMENT, `username` VARCHAR(30) NOT NULL, `login_date` datetime NOT NULL, PRIMARY KEY(`logid`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Inserting Data Into a Database
Next Step in to insert some data into our database. This will serve as our reference when we select our month and year.
1. Click the database “month_year” that we have created earlier.
2. Click SQL and paste the code below.
INSERT INTO `login` (`username`, `login_date`) VALUES ('nurhodelta', '2017-08-22 07:10:00'), ('lee', '2017-05-22 08:30:00'), ('nurhodelta', '2017-08-22 13:15:00'), ('lee', '2017-03-22 14:00:00'), ('nurhodelta', '2017-05-16 10:30:00'), ('lee', '2017-08-15 20:00:00');
Creating a Connection
Next step is to create a database connection and save it as “conn.php”. This file will serve as our bridge between our form and our database. To create the file, open your HTML code editor and paste the code below after the tag.
<?php //MySQLi Procedural $conn = mysqli_connect("localhost","root","","month_year"); if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } //MySQLi Object-oriented //$conn = new mysqli("localhost","root","","month_year"); //if ($conn->connect_error) { // die("Connection failed: " . $conn->connect_error); //} ?>
Creating a Form and Table
Lastly, we create our login table, our form and our result table on one page. To create the page, open your HTML code editor and paste the code below after the tag. We name this page as “index.php”.
<!DOCTYPE html> <html> <head> <title>Select Data By Month and Year</title> </head> <body> <h2>Login Table</h2> <div> <table border="1"> <thead> <th>UserID</th> <th>Username</th> <th>Login Date</th> </thead> <tbody> <?php include('conn.php'); //MySQLi Procedural $query=mysqli_query($conn,"select * from `login`"); while($row=mysqli_fetch_array($query)){ ?> <tr> <td><?php echo $row['logid']; ?></td> <td><?php echo $row['username']; ?></td> <td><?php echo $row['login_date']; ?></td> </tr> <?php } //MySQLi Object-oriented //$query=$conn->query("select * from `login`"); //while($row = $query->fetch_array()) { /* ?> <tr> <td><?php echo $row['logid']; ?></td> <td><?php echo $row['username']; ?></td> <td><?php echo $row['login_date']; ?></td> </tr> <?php */ //} ?> </tbody> </table> </div><br> <div> <form method="POST"> <label>Month: </label> <select name="month"> <?php for ($i = 1; $i <= 12; $i++) { $month = date('F', mktime(0, 0, 0, $i, 1, 2011)); ?> <option value="<?php echo $i; ?>"><?php echo $month; ?></option> <?php } ?> </select> <label>Year: </label> <select name="year"> <?php for($n=2017;$n<=2050;$n++){ ?> <option value="<?php echo $n; ?>"><?php echo $n; ?></option> <?php } ?> </select> <input type="submit" value="Get Data" name="submit"> </form> </div> <h2>Data in Selected Month and Year</h2> <div> <table border="1"> <thead> <th>UserID</th> <th>Username</th> <th>Login Date</th> </thead> <tbody> <?php if (isset($_POST['submit'])){ include('conn.php'); $month=$_POST['month']; $year=$_POST['year']; //MySQLi Procedural $oquery=mysqli_query($conn,"select * from `login` where month(login_date)='$month' and year(login_date)='$year'"); if (mysqli_num_rows($oquery) <=0){ echo "No data Found."; } else{ while($orow=mysqli_fetch_array($oquery)){ ?> <tr> <td><?php echo $orow['logid']?></td> <td><?php echo $orow['username']?></td> <td><?php echo $orow['login_date']?></td> </tr> <?php } } //MySQLi Object-oriented //$oquery=$conn->query("select * from `login` where month(login_date)='$month' and year(login_date)='$year'"); //if ($oquery->num_rows <= 0) { // echo "No data Found."; //} //else{ //while($orow = $oquery->fetch_array()){ /* ?> <tr> <td><?php echo $orow['logid']?></td> <td><?php echo $orow['username']?></td> <td><?php echo $orow['login_date']?></td> </tr> <?php */ //} //} } ?> </tbody> </table> </div> </body> </html>
Output
Happy Coding!
Related Tutorials: How to Select Data Between Two Dates in PHP/MySQL, Save Selected Options using AngularJS and PHP/MySQLi, Update and Delete Data using VueJs in PHP, Drop Down Filter Selection with MySQL in PHP, Populate Select Tag Base On Category in PHP, Select MySQLi Data By Monthly using PHP, Filter Range Of Date With MySQLi using PHP, How to Upload and Validate an Image File in PHP