How $_GET Variable Works and How to Use It in PHP/MySQL

Language

This tutorial will show you how does $_GET method works and how to use it. $_GET is a kind of method in getting a particular value in the webpage's url. This will be possible by sending this particular variable in the url of the "go to" page. In this tutorial, I will show you how to send a value in your url and getting that particular value via $_GET method.

Creating our Database

First, we're going to create a database that contains the user data. 1. Open phpMyAdmin. 2. Click databases, create a database and name it as "get". 3. After creating a database, click the SQL and paste the below code. See image below for detailed instruction.
  1. CREATE TABLE `user` (
  2. `userid` INT(11) NOT NULL AUTO_INCREMENT,
  3. `firstname` VARCHAR(30) NOT NULL,
  4. `lastname` VARCHAR(30) NOT NULL,
  5. PRIMARY KEY (`userid`)
  6. ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
get

Inserting Data into our Database

Next, we insert data into our database. These data will serve as our reference. 1. Click our database "get". 2. Click SQL and paste the below code.
  1. INSERT INTO `user` (`firstname`, `lastname`) VALUES
  2. ( 'neovic', 'devierte'),
  3. ( 'lee', 'ann');

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 page and our database. To create the file, open your HTML code editor and paste the code below after the tag.
  1. <?php
  2. $conn = mysqli_connect("localhost","root","","get");
  3.  
  4. // Check connection
  5. {
  6. echo "Failed to connect to MySQL: " . mysqli_connect_error();
  7. }
  8. ?>

Creating our Reference Table

Next step is to create our reference table and name it as "index.php". This table contains the data that we inserted in our database earlier. To create the table, open your HTML code editor and paste the code below after the tag.
  1. <!DOCTYPE html>
  2. <title>$_GET tutorial</title>
  3. </head>
  4. <table border="1">
  5. <th>Firstname</th>
  6. <th>Lastname</th>
  7. <th></th>
  8. </thead>
  9. <h2>User Table</h2>
  10. <?php
  11. include('conn.php');
  12. $query=mysqli_query($conn,"select * from `user`");
  13. while($row=mysqli_fetch_array($query)){
  14. ?>
  15. <tr>
  16. <td><?php echo $row['firstname']; ?></td>
  17. <td><?php echo $row['lastname']; ?></td>
  18. <td><a href="goto.php?id=<?php echo $row['userid']; ?>">View</a></td>
  19. </tr>
  20. <?php
  21. }
  22. ?>
  23. </tbody>
  24. </table>
  25. </body>
  26. </html>

Creating our Go to Page

Lastly, we create our go to page and name it as "goto.php". This page will received the value that we are sending and get that value using the $_GET method. To create the page, open your HTML code editor and paste the code below after the tag.
  1. <!DOCTYPE html>
  2. <title>$_GET tutorial</title>
  3. </head>
  4. <?php
  5. include('conn.php');
  6. $id=$_GET['id'];
  7. $query=mysqli_query($conn,"select * from `user` where userid='$id'");
  8. $row=mysqli_fetch_array($query);
  9. ?>
  10. <h2></h2>
  11. You Click: <?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?>
  12. </body>
  13. </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