0

I'm new to this PDO stuff with PHP so I want to know how to assign the data to variables.

I'm getting this Array ( [0] => Array ( [id] => 1 [title] => Test Announcement! [link] => http://www.google.com ) ) when it returns.

I want it to be just the "Test Announcement" and "http://www.google.com" and be able to sign them to a variable like $title and $link.

functions.php

<?php
    require("common.php");
    function getAnnouncements() {
        $query = "SELECT title, link FROM announcements";

        try {
            global $db;
            // Execute the query against the database
            $stmt = $db->prepare($query); 
            $stmt->execute();
            $result = $stmt->fetchAll();
            print_r($result);

        }
        catch(PDOException $ex) { 
            // Note: On a production website, you should not output $ex->getMessage(). 
            // It may provide an attacker with helpful information about your code.  
            die("Error loading announcements"); 
        } 

    }

?>

I used kypros answer and now I have this:

<?php
    require("common.php");
    function getAnnouncements() {
        $query = "SELECT title, link FROM announcements";

        try {
            global $db;
            // Execute the query against the database
            $stmt = $db->prepare($query); 
            $stmt->execute();
            $result = $stmt->fetchAll();
            foreach($result as $announcement)
                $title = $announcement['title'];
                $link = $announcement['link'];
                echo "<a href='".$link."'>".$title."</a>";
            }
        }
        catch(PDOException $ex) { 
            // Note: On a production website, you should not output $ex->getMessage(). 
            // It may provide an attacker with helpful information about your code.  
            die("Error loading announcements"); 
        } 

    }

?>

With a error.log and blank white page.

[18-Oct-2014 13:28:12 UTC] PHP Parse error:  syntax error, unexpected '}', expecting T_CATCH in /home/dxbridge/public_html/scripts/functions.php on line 17
[18-Oct-2014 13:28:13 UTC] PHP Parse error:  syntax error, unexpected '}', expecting T_CATCH in /home/dxbridge/public_html/scripts/functions.php on line 17
Joel Evans
  • 29
  • 4
  • Sorry, I tried searching around. I'll check that out, thanks. – Joel Evans Oct 18 '14 at 13:19
  • Joel No problem, i also added a specific answer as well you can reference to get to where you want to be. @MichaelBerkowski Doing that if he has many announcements he will only get the first rather than looping through all of them. – Kypros Oct 18 '14 at 13:25

1 Answers1

0

Your $result now contains all rows matching your select statement. To access the individual column values you could do something like:

foreach($result as $announcement)
{
    $title = $announcement['title'];
    $link = $announcement['link'];
    echo "<a href='".$link."'>".$title."</a>";
}

This will output a hyperlink linking each announcement to a link and showing only it's title

Kypros
  • 2,997
  • 5
  • 21
  • 27