How to pull data from different mysql databases and display it (using pdo mysql) based on user clicking a predefined link on the webpage

Okay, so I am still learning PHP and pdo mysql, so what I am really looking for here is guidance\examples on basic principles of how to achieve my goal, this way I can make changes down the road on my own.

What I already have is a website where users (public users, no logins) will visit the page and can see the latest News from my database. As an example, if they load the page, it will display all news in the database named ‘nov15_announce’ and display it in descending order by date, and it limits it to the most recent 5.

Currently, I have links at the top of the page that they can click to see previous months announcements. When they click the link, it loads a separate webpage that I created with modified code to display data from that particular database. So, when they click “September” link, it loads the webpage that pulls data from the database named ‘sep15_announce’. When they click “October” link it takes them to the webpage that pulls data from the database named ‘oct15_announce’, etc…

What I would like to be able to do is instead of having multiple webpages with their own pdo code specifically for that database, have one webpage, that pulls & displays data based on the user selecting a link such as “September”, “October”, “November” etc etc…

Hopefully, I have thoroughly explained my intent. I am looking forward to learning!
Thanks in advance!

If you have a link something like www.mysite.com?date=sep15_announce
This is just a simple example and you can generate you link with variables etc.

On your next page you would have:

$date = $_GET['sep15_announce'];

Then use $date in your query to display the results on the page. You would need to add validation to $date to make sure the data passed is safe.

This is a simple explanation as the details can be done in different ways.

Thanks for the reply!

Just for clarification, are you saying that I can do this all on one page? Or are you meaning that I put the

[quote=“Rubble, post:2, topic:206598”]
$date = $_GET[‘sep15_announce’];
[/quote] on a separate page?

I am obviously not understanding the concept here, because I can not get it to work. Here is what I have so far…The first if statement doesnt do anything (Which is what I added for what Im trying to accomplish here), the second half does however work.

<h4 class="heading-title pull-left"><a href="announcementsSEP.php"> September | </a><a href="announcementsOCT.php"> October | <a href="announcementsNOV.php"> November | </a> <a href="ABCannouncements_november15TEST.php?date=pto_announce"> TEST to PTO | </a></h4>

  <?php
include "assets/config/dblogincred.php";
$usertable='nov_announce';
$date = $_GET['pto_announce'];

 $id = 1;

if $date=pto_announce then
{
try {
  $conn = new PDO("mysql:host=$dbhost;dbname=$mydb", $dbuser, $dbpass);

  $stmt = $conn->prepare('SELECT * FROM pto_announce WHERE 1 ORDER by news_date desc limit 9');
  $stmt->execute(array('id' => $id));
  $result = $stmt->fetchAll();
 
  if ( count($result) ) { 
    foreach($result as $row) {


print_r('<div class="panel panel-default page-row">' . '<div class="panel-heading">' . '<h3 class="panel-title">');
print_r('<b>' . $row['news_heading'] . ' - ' );
print_r('<font size="2"><a><i> posted ' . $row['news_date'] . '</i></a></font></b></h3>' );
print_r('</div><div class="panel-body"><p>');
print_r($row['news_body'] . '<br></p></div></div><!--//panel-->');

}
else
try {
  $conn = new PDO("mysql:host=$dbhost;dbname=$mydb", $dbuser, $dbpass);

  $stmt = $conn->prepare('SELECT * FROM nov_announce WHERE 1 ORDER by news_date desc limit 9');
  $stmt->execute(array('id' => $id));
  $result = $stmt->fetchAll();
 
  if ( count($result) ) { 
    foreach($result as $row) {


print_r('<div class="panel panel-default page-row">' . '<div class="panel-heading">' . '<h3 class="panel-title">');
print_r('<b>' . $row['news_heading'] . ' - ' );
print_r('<font size="2"><a><i> posted ' . $row['news_date'] . '</i></a></font></b></h3>' );
print_r('</div><div class="panel-body"><p>');
print_r($row['news_body'] . '<br></p></div></div><!--//panel-->');

                              
                            
}   
  } 
else {
    echo "No rows returned.";
  }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
?>

Okay, so I finally figured it out. I was going about it the wrong way…but your suggestion was spot on. In case someone else comes across this post having the same question as I did, I am posting my working php below:

<a href="myscript3.php?monthdb=pto_announce">PTO NEWS</a><br><br>

<a href="myscript3.php?monthdb=nov_announce">November Announcements</a><br><br>

<?php

echo "Successfully fetched - $_GET[monthdb] (This is here for Troubleshooting purposes)<br><br>";

include "assets/config/dblogincred.php";
$usertable='nov_announce';

 
 $id = 1;

if($_GET["monthdb"] === "nov_announce") echo "You have selected Nov NEWS<br><br>";
if($_GET["monthdb"] === "pto_announce") echo "You have selected PTO NEWS<br><br>";
if($_GET["monthdb"] === "pto_announce") {
try {
  $conn = new PDO("mysql:host=$dbhost;dbname=$mydb", $dbuser, $dbpass);

  $stmt = $conn->prepare('SELECT * FROM pto_announce WHERE 1 ORDER by news_date desc limit 9');
  $stmt->execute(array('id' => $id));
  $result = $stmt->fetchAll();
 
  if ( count($result) ) { 
    foreach($result as $row) {


print_r('<div class="panel panel-default page-row">' . '<div class="panel-heading">' . '<h3 class="panel-title">');
print_r('<b>' . $row['news_heading'] . ' - ' );
print_r('<font size="2"><a><i> posted ' . $row['news_date'] . '</i></a></font></b></h3>' );
print_r('</div><div class="panel-body"><p>');
print_r($row['news_body'] . '<br></p></div></div><!--//panel-->');

                              
                            
}   
  } 
     
else {
    echo "No rows returned.";
  }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
}

elseif($_GET["monthdb"] === "nov_announce") {
try {
  $conn = new PDO("mysql:host=$dbhost;dbname=$mydb", $dbuser, $dbpass);

  $stmt = $conn->prepare('SELECT * FROM nov_announce WHERE 1 ORDER by news_date desc limit 9');
  $stmt->execute(array('id' => $id));
  $result = $stmt->fetchAll();
 
  if ( count($result) ) { 
    foreach($result as $row) {


print_r('<div class="panel panel-default page-row">' . '<div class="panel-heading">' . '<h3 class="panel-title">');
print_r('<b>' . $row['news_heading'] . ' - ' );
print_r('<font size="2"><a><i> posted ' . $row['news_date'] . '</i></a></font></b></h3>' );
print_r('</div><div class="panel-body"><p>');
print_r($row['news_body'] . '<br></p></div></div><!--//panel-->');

                              
                            
}   
  } 
     
else {
    echo "No rows returned.";
  }
} catch(PDOException $e) {
    echo 'ERROR: ' . $e->getMessage();
}
}
?>

Thank you “Rubble” for your suggestion!!

I am glad you made it work and posted you method to help others @ParatrooperJoe

Do not forget some validation of monthdb to prevent hacking.
I would check its length and make sure it contains what you expect - just alphanumeric characters. One better method is to get all the options from your database for mothdb at the top of the page in a database query. You can then check that the value of mothdb passed in the URL is in that array before you proceed.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.