Use $_GET[‘id’] to Pass a PHP/MySQL Variable

Here's a simple explanation on extracting data from your MySQL database and using the all powerful GET method to move the data from page to page on your website.

One of the most popular questions I get about php is how to pass a variable from one page to another. The GET and POST methods can be a little bit confusing at first but once you grasp how to use these, building a website becomes a pretty fun process. This post presents a very simplistic approach to using the GET method.

I’ll preface this example by telling you that most of the money I make online is usinig the GET method. All you need is a MySQL database full of records and the GET method allows you to extract all of your information. Create some pagination and you have a website with hundreds or thousands of database records. I highly encourage everyone to learn PHP and start figuring out how to pull MySQL data to your website pages.

And now for the example..

First, we’ll create a webpage called phptest1.php and pull some records to it from our database. To do this, we need to first connect to the database and then query it (If you don’t know how to connect to a mysql database, I have example code in my rss feed article). Copy the following to phptest1.php and upload it to your web directory:

<?
// config.php has our database connection information so we have to call it first:
INCLUDE ‘config.php’;

//we assign the variable from the config.php to $link
$link = mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name, $link);

//For this example, we’re going to pull 10 records from the database (LIMIT 0 , 10):
$sql_statement = ‘SELECT TITLE, description, SOURCE FROM things ORDER BY DATE DESC LIMIT 0 , 10’;
$result = mysql_query($sql_statement, $link);

//now we create a while “loop” to fetch all 10 records:
while ($curr_row = mysql_fetch_assoc($result)) {

// This next line just simply pulls the title from our database and displays it:
echo $title;
echo ‘<br><br>’;

//This one shows how to concantenate php variables with html.. pulling both the title and
description from our database:
echo ‘<font SIZE=”2″ face=”sans-serif”><b><h5>’ . $curr_row[‘title’] . ‘</h5></b>’ .
‘&nbsp;&nbsp;’ . $curr_row[‘description’] . ‘…</font><br><br>’;

//And this line is the one that we’ll use to create our new page. It’s a hyperlink that is calling our new page “phptest2.php” and populating it with the title.
echo “<a href=’phptest2.php?id=” . $curr_row[‘title’] . “‘>”. $curr_row[‘title’] .”</a>”;

} // This closed bracket ends our WHILE LOOP. So long as there are conditions to be met above this bracket, they will run until finished and then continue past this.

mysql_free_result($result); // Release our sql query. It’s not necessary, just good
programming.
?>

Okay.. so there’s the main page of our site. You can pull as few or as many records as you want to it, but better now is that we simply create phptest2.php. As you can see above, we call this page in our hyperlink and it’s all that’s needed to display all the records in our database as unique pages. With only 2 pages uploaded, we can have a website with thousands of pages, as they’re all dynamically created and just representing each individual record in our database. Style your website and you’re all finished.

Create phptest2.php and it’s as short and simple as adding this to it:

<?
$title=$_GET[‘id’];
echo $title;
?>

Play around with it. Format your rows, send different ids through your url and mix up your result set. If you learn how to do the above, you’re well on your way to becoming a php programmer.

Submit a comment

CommentLuv badge

reverse phone lookupTattoo DesignsSEO
My New Amazon Script Is Close!
A new php script for sale is coming!
Sign up to get notified

..