How to Select Data By Month and Year in PHP/MySQL

Language

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 is on How to Select Data by Month and Year using PHP. Also, in this tutorial, I've created two mysqli 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.
  1. CREATE TABLE `login` (
  2. `logid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `username` VARCHAR(30) NOT NULL,
  4. `login_date` datetime NOT NULL,
  5. PRIMARY KEY(`logid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
month

Inserting Data Into our 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.
  1. INSERT INTO `login` (`username`, `login_date`) VALUES
  2. ('nurhodelta', '2017-08-22 07:10:00'),
  3. ('lee', '2017-05-22 08:30:00'),
  4. ('nurhodelta', '2017-08-22 13:15:00'),
  5. ('lee', '2017-03-22 14:00:00'),
  6. ('nurhodelta', '2017-05-16 10:30:00'),
  7. ('lee', '2017-08-15 20:00:00');

Creating our 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.
  1. <?php
  2.  
  3. //MySQLi Procedural
  4. $conn = mysqli_connect("localhost","root","","month_year");
  5. if (!$conn) {
  6. die("Connection failed: " . mysqli_connect_error());
  7. }
  8.  
  9. //MySQLi Object-oriented
  10. //$conn = new mysqli("localhost","root","","month_year");
  11. //if ($conn->connect_error) {
  12. // die("Connection failed: " . $conn->connect_error);
  13. //}
  14.  
  15. ?>

Creating our 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".
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <title>Select Data By Month and Year</title>
  5. </head>
  6. <body>
  7. <h2>Login Table</h2>
  8. <div>
  9. <table border="1">
  10. <thead>
  11. <th>UserID</th>
  12. <th>Username</th>
  13. <th>Login Date</th>
  14. </thead>
  15. <tbody>
  16. <?php
  17. include('conn.php');
  18. //MySQLi Procedural
  19. $query=mysqli_query($conn,"select * from `login`");
  20. while($row=mysqli_fetch_array($query)){
  21. ?>
  22. <tr>
  23. <td><?php echo $row['logid']; ?></td>
  24. <td><?php echo $row['username']; ?></td>
  25. <td><?php echo $row['login_date']; ?></td>
  26. </tr>
  27. <?php
  28. }
  29.  
  30. //MySQLi Object-oriented
  31. //$query=$conn->query("select * from `login`");
  32. //while($row = $query->fetch_array()) {
  33. /* ?>
  34. <tr>
  35. <td><?php echo $row['logid']; ?></td>
  36. <td><?php echo $row['username']; ?></td>
  37. <td><?php echo $row['login_date']; ?></td>
  38. </tr>
  39. <?php */
  40. //}
  41. ?>
  42. </tbody>
  43. </table>
  44. </div><br>
  45. <div>
  46. <form method="POST">
  47. <label>Month: </label>
  48. <select name="month">
  49. <?php
  50.  
  51. for ($i = 1; $i <= 12; $i++)
  52. {
  53. $month = date('F', mktime(0, 0, 0, $i, 1, 2011));
  54. ?>
  55. <option value="<?php echo $i; ?>"><?php echo $month; ?></option>
  56. <?php
  57. }
  58.  
  59. ?>
  60. </select>
  61.  
  62. <label>Year: </label>
  63. <select name="year">
  64. <?php
  65. for($n=2017;$n<=2050;$n++){
  66. ?>
  67. <option value="<?php echo $n; ?>"><?php echo $n; ?></option>
  68. <?php
  69. }
  70. ?>
  71. </select>
  72. <input type="submit" value="Get Data" name="submit">
  73. </form>
  74. </div>
  75. <h2>Data in Selected Month and Year</h2>
  76. <div>
  77. <table border="1">
  78. <thead>
  79. <th>UserID</th>
  80. <th>Username</th>
  81. <th>Login Date</th>
  82. </thead>
  83. <tbody>
  84. <?php
  85. if (isset($_POST['submit'])){
  86. include('conn.php');
  87. $month=$_POST['month'];
  88. $year=$_POST['year'];
  89.  
  90. //MySQLi Procedural
  91. $oquery=mysqli_query($conn,"select * from `login` where month(login_date)='$month' and year(login_date)='$year'");
  92. if (mysqli_num_rows($oquery) <=0){
  93. echo "No data Found.";
  94. }
  95. else{
  96. while($orow=mysqli_fetch_array($oquery)){
  97. ?>
  98. <tr>
  99. <td><?php echo $orow['logid']?></td>
  100. <td><?php echo $orow['username']?></td>
  101. <td><?php echo $orow['login_date']?></td>
  102. </tr>
  103. <?php
  104. }
  105. }
  106.  
  107. //MySQLi Object-oriented
  108. //$oquery=$conn->query("select * from `login` where month(login_date)='$month' and year(login_date)='$year'");
  109. //if ($oquery->num_rows <= 0) {
  110. // echo "No data Found.";
  111. //}
  112. //else{
  113. //while($orow = $oquery->fetch_array()){
  114. /* ?>
  115. <tr>
  116. <td><?php echo $orow['logid']?></td>
  117. <td><?php echo $orow['username']?></td>
  118. <td><?php echo $orow['login_date']?></td>
  119. </tr>
  120. <?php */
  121. //}
  122. //}
  123. }
  124. ?>
  125. </tbody>
  126. </table>
  127. </div>
  128. </body>
  129. </html>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment