encrypted21 encrypted21 - 2 years ago 89
PHP Question

How this PHP code should look like to filter out displayed SQL rows if link is clicked?

I have a homework where I have to call all the rows from the database. When an id of each row is clicked, there should be only this that row displaying.
There are three rows which contain text and two columns: id INT NOT NULL AUTO_INCREMENT and content VARCHAR(255).

I have this code:

file: db.php


define("dbserver", "");

define("dbuser", "root");

define("dbpass", "");

define("dbname", "dbtesting");

$db = new PDO(

"mysql:host=" .dbserver. ";dbname=" .dbname,dbuser,dbpass,







file: index.php


include "db.php";

function articleList($get = 0) {

global $db;

if (empty($get["p"])) {
$sql = "SELECT * FROM dbtesting.articles";
else {
$get2 = $get["p"];
$sql = "SELECT * FROM dbtesting.articles WHERE id = '$get2'";

$result = $db->query($sql);

foreach ($result as $value) {
echo "<div style='background-color:#dedede;margin-top:10px'>";
echo "<a href='index.php?p=".$value["id"]."'>"."ID"."</a>";
echo " - ";
echo $value["content"];
echo "</div>"."<br>";




The target is to make SQL to display only one article whom "ID" link has been clicked as shown in the picture below. No JavaScript or other languages are allowed. I have no idea how this should work.
This code has been written by my teacher and there is something I should fix for this to work.
I only remember him telling that this should be done by using get parameter or something. I didn't really understand. Could you please help to make it work?

Rows (articles) are displayed. When I click on the ID link there is no effect except the URL change. But content isn't changing.

All articles are displayed - working

all articles

Only clicked article should be displayed (
in URL) - not working

Answer Source

try use $_GET["p"] not $get...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download