Skip to main content

PHP - Select MySQLi Data By Monthly


In this tutorial we will create a Select MySQLi Data By Monthly using PHP. This code will select a specific MySQLi table row when user submit a month and year from a form. The code use MySQLi SELECT query to select a data in the MySQLi row and by providing a month and year in the WHERE clause it will display that particular data base 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 webserver including xamp, wamp, etc. It is being use to any famous websites because of modern approach as its today.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for 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.
tut1

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.

  1. <?php
  2. $conn=mysqli_connect("localhost", "root", "", "db_month");
  3.  
  4. if(!$conn){
  5. die("Error: Failed to connect to database!");
  6. }
  7. ?>

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.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5. <link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
  6. </head>
  7. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">PHP - Select MySQLi Data By Monthly</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-2"></div>
  18. <div class="col-md-8">
  19. <form method="POST" action="">
  20. <div class="form-inline">
  21. <label>Month: </label>
  22. <select name="month" class="form-control">
  23. <?php
  24. for ($i = 1; $i <= 12; $i++){
  25. $month = date('F', mktime(0, 0, 0, $i, 1, 2019));
  26. ?>
  27. <option value="<?php echo $i; ?>"><?php echo $month; ?></option>
  28. <?php
  29. }
  30. ?>
  31. </select>
  32. <label>Year: </label>
  33. <select name="year" class="form-control">
  34. <?php
  35. for ($i = date('Y'); $i >= 1965; $i--){
  36. ?>
  37. <option value="<?php echo $i; ?>"><?php echo $i; ?></option>
  38. <?php
  39. }
  40. ?>
  41. </select>
  42. <button class="btn btn-primary" name="search">Search</button>
  43. </div>
  44. </form>
  45. <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>
  46. <table class="table table-bordered">
  47. <thead class="alert-info">
  48. <tr>
  49. <th>Firstname</th>
  50. <th>Lastname</th>
  51. <th>Data Joined</th>
  52. </tr>
  53. </thead>
  54. <tbody>
  55. <?php include'search.php'?>
  56. </tbody>
  57. </table>
  58. </div>
  59. </div>
  60. </body>
  61. </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

  1. <?php
  2. require'conn.php';
  3.  
  4. if(ISSET($_POST['search'])){
  5. $month=$_POST['month'];
  6. $year=$_POST['year'];
  7. $query=mysqli_query($conn, "SELECT * FROM `member` WHERE month(date_joined)='$month' AND year(date_joined)='$year'") or die(mysqli_error());
  8. while($fetch=mysqli_fetch_array($query)){
  9. echo "<tr><td>".$fetch['firstname']."</td><td>".$fetch['lastname']."</td><td>".$fetch['date_joined']."</td></tr>";
  10. }
  11. }else{
  12. $query=mysqli_query($conn, "SELECT * FROM `member`") or die(mysqli_error());
  13. while($fetch=mysqli_fetch_array($query)){
  14. echo "<tr><td>".$fetch['firstname']."</td><td>".$fetch['lastname']."</td><td>".$fetch['date_joined']."</td></tr>";
  15. }
  16. }
  17. ?>

There you have it we successfully created Select MySQLi Data By Monthly using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

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

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.