Google Ads

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


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

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.

 

 

CREATE TABLE `user` (
`userid` INT(11) NOT NULL AUTO_INCREMENT,
`firstname` VARCHAR(30) NOT NULL,
`lastname` VARCHAR(30) NOT NULL,
PRIMARY KEY (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

 

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.

 

INSERT INTO `user` (`firstname`, `lastname`) VALUES
( 'neovic', 'devierte'),
( '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.

 

<?php
$conn = mysqli_connect("localhost","root","","get");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
?>

 

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.

 

<!DOCTYPE html>
<html>
<head>
<title>$_GET tutorial</title>
</head>
<body>
<table border="1">
<thead>
<th>Firstname</th>
<th>Lastname</th>
<th></th>
</thead>
<tbody>
<h2>User Table</h2>
<?php
include('conn.php');
$query=mysqli_query($conn,"select * from `user`");
while($row=mysqli_fetch_array($query)){
?>
<tr>
<td><?php echo $row['firstname']; ?></td>
<td><?php echo $row['lastname']; ?></td>
<td><a href="goto.php?id=<?php echo $row['userid']; ?>">View</a></td>
</tr>
<?php
}
?>
</tbody>
</table>
</body>
</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.

 

<!DOCTYPE html>
<html>
<head>
<title>$_GET tutorial</title>
</head>
<body>
<?php
include('conn.php');
$id=$_GET['id'];
$query=mysqli_query($conn,"select * from `user` where userid='$id'");
$row=mysqli_fetch_array($query);
?>
<h2></h2>
You Click: <?php echo $row['firstname']; ?> <?php echo $row['lastname']; ?>
</body>
</html>

 

 


Related Post

9apps Graber v2 PHP Script 9apps Graber v2 PHP Script Free Download PHP Script From our site. Here is many php script. You can search php script, theme plugin module coding m...
Web Radio Portal v1.1 PHP Script Download Web Radio Portal v1.1Web Radio Portal v1.1Web Radio portal is simple and elegant script which provide streams of unlimited radio stations around the w...
Super Green PHP Script Download Super GreenSuper GreenHelp Green to save his girlfriend. How to play: For playing use touch or keyboard. Enjoy…Demo: http://codecanyon.net/item/supe...
Simple Mapping System using PHP and JQuery PHP Scr... Simple Mapping System using PHP and JQuery PHP Script In this tutorial, we are going to learn to learn how to make a drag and drop system and save ...
Sticky HTML5 Music Player With Continuous Playback... Sticky HTML5 Music Player With Continuous PlaybackSticky HTML5 Music Player With Continuous PlaybackThis HTML5 Audio Player is ideal to add background...

Have any Question or Comment?

Leave a Reply

Your email address will not be published. Required fields are marked *