Jump to content

How to activate a link on a website that will grab data from a database?


Recommended Posts

Hi there,

I am new to php. I am designing a website that is linked to a database using php and mySQL. I have a table in my database that has 4 columns. This table contains data about history articles. One of the column names in the table is called "century". I am creating ahref links on the website I would like to link to a list different articles from differnt centuries. For example if someone clicked on a 10th century link the webpage would then communicate with the database and grab all the 10th centuries articles and list them on this page or another page. I know that a mySQL command something like this

SELECT * FROM articles WHERE century = tenth;

would list all the articles I need but I dont know if I put this command inbetween an a href or do I need to write some sort of php file? If you know what Im on about would you be able to give me an example script of whatever I need to do. Your help would be much appreciated.

Rgds.
One way is to use the $_GET method to do this.

your link might be index.php?century=tenth

on the PHP end, the variable called $_GET['century'] would be "tenth"

you can then use that variable in your mysql query:

$result=mysql_query(SELECT * FROM `articles` WHERE `century` = '".$_GET['century']."'");
Thanks v much for that. I sort of understand what you are saying but as im am very new to programming I dont know how to put this into effect. I have the html page displaying just general info at the moment. Do i use an ahref link here? I dont know how to code in php yet. Do I have a separate php file and what should it look like if I was to just list the 10th century articles in a simple html page containing just the links.

Sorry for being so green.
Rgds
you can write php in line with your html. check the $_GET variables to see what the "century" is, then run your mysql_query to capture that information.

links would read "index.php?century=tenth" or whatever... instead of "tenthcentury.html"

you might want to dig around for some beginner tutorials, though. You've got a long way to go.
Thanks again Michael. I now understand that my links will be "index.php?century=tenth" or similar. When someone then clicks on this link I have discovered that a php file will need to be called. This php file i guess will have what you explain below.
--------
Coonect to database

the variable called $_GET['century'] would be "tenth"

you can then use that variable in your mysql query:

$result=mysql_query(SELECT * FROM `articles` WHERE `century` = '".$_GET['century']."'");
--------

As this is all I need to display on this site on a simple page would you be able to help me put together a simple php file that will just connect me to a database and then grab the century columns articles and output them. The century column articles will already be urls. I really appreaciate your help so far.


Hi
Can anyone help me finish off this? Here is what I have put together so far. When someone clicks on say the '10th century articles' link my webpage I want to connect to the database and then grab whatever articles are from the tenth century. The database will have a column called century and in this column I will have tenth, eleventh and so on...depending on what century the article url is about.

<html>
<head><title>List of articles</title></head>
<body>
<h1>List of articles about 10th Century</h1>
<?php
$db = mysql_connect(“localhost”, “username”, “password”);
mysql_select_db(“databasename”, $db);


/// How do I use $_GET method to check if the century in my table states for example“tenth” or “eleventh”
/// etc.


$result = mysql_query(“SELECT * FROM SELECT `articles` WHERE `century` = '".$_GET['century'],"databasename");

?>

<table rules=all>
<thead>
<tr><th>Column1</th><th>Column2</th><th>Column3</th></tr>
</thead>
<tbody>

<?php
While ($myrow = mysql_fetch_row($result))
{printf (“<tr><td>%s</td><td>%s</td><td>%s</td></tr>\n,
$myrow[0], $myrow[1], $myrow[2]);
?>
</tbody>
<.table>
</body>
</html>


On my main page I have an <a href="index.php?century=tenth">10 Century Articles</a>

Why do I name in this way to link? Basically what I want to happen is as follows:

Someone visits my page. Click on a link called 'view all articiles that are about the tenth century'. The site will connect to database. select all tenth century articles and then display on website. It will for example display in 3 column ...one for article name one for article url and one for whatever. These columns will also be in database.

Maybe someone could help me out with a snippet of code that I may have wrong or am missing??

Cheers.
Your question: How do I use $_GET method to check if the century in my table states for example“tenth” or “eleventh” etc.

This is a basic tutorial question. you really should go through the tutorial. Here is a brief overview in my own connotative:

The URL index.php?century=tenth is assigning the variable "century" the value "tenth". You can continue to assign variables in the same manner using the ampersand after the first variable. So, index.php?century=tenth&number=10 is assigning two $_GET variables: century and number.

you can access the $_GET variables inside PHP as an array, putting the value of the variable you want inside of the brackets. so $_GET['century'] would return "tenth" in our case.


This rewritten code should work -- provided your database has content in a table called "articles", and the century field is actually called "century". Also, I have not touched your HTML, except to properly close your table tag.

[code]<html>
<head><title>List of articles</title></head>
<body>
<h1>List of articles about 10th Century</h1>
<?php
$db = mysql_connect(“localhost”, “username”, “password”);
mysql_select_db(“databasename”, $db);
///updated the following code to be correct:
$result = mysql_query(“SELECT * FROM `articles` WHERE `century` = '".$_GET['century']."'");

?>

<table rules=all>
<thead>
<tr><th>Column1</th><th>Column2</th><th>Column3</th></tr>
</thead>
<tbody>

<?php
while ($myrow = mysql_fetch_row($result)) {
    echo “<tr><td>".$myrow[0]."</td><td>".$myrow[1]."</td><td>".$myrow[2]."</td></tr>\n";
}
?>
</tbody>
</table>
</body>
</html>
[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.