Skip to content
Snippets Groups Projects
Blog_Entry_Table.class.php 1.12 KiB
Newer Older

class Blog_Entry_Table {

    //Establish connection to database with PDO
    private $db;
    public function __construct ( $db ) {
        $this->db = $db;
    }
    
    //Insert new entries
    public function saveEntry ( $title, $text ) {
Tran Le's avatar
Tran Le committed
        $entrySQL = "INSERT INTO blog_entry (title, text) VALUES (?, ?)";
        $entryStatement = $this->db->prepare( $entrySQL );
        $formData = array( $title, $entry ); 

        try {
            $entryStatement->execute( $formData );
        } catch (Exception $e){
            $msg = "<p>You tried to run this sql: $entrySQL<p>
                    <p>Exception: $e</p>";
            trigger_error($msg);
        }
    }

    //Get all entries
     public function getAllEntries () {
Tran Le's avatar
Tran Le committed
        $entrySQL = "SELECT id, title, SUBSTRING(text, 1, 140) AS intro FROM blog_entry";
        $statement = $this->db->prepare( $entrySQL );
        try{
            $statement->execute();
        } catch (Exception $e){
            $msg = "<p>You tried to run this sql: $entrySQL<p>
                    <p>Exception: $e</p>";
            trigger_error($msg);
        }
    return $statement;
    }

}

?>