Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // include and register Twig auto-loader
- include 'Twig/Autoloader.php';
- Twig_Autoloader::register();
- // attempt a connection
- try {
- $dbh = new PDO('mysql:dbname=employedb1;host=localhost', 'test', 'test');
- } catch (PDOException $e) {
- echo "Error: Could not connect. " . $e->getMessage();
- }
- // set error mode
- $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- // attempt some queries
- try {
- // execute SELECT query
- // store each row as an object
- $sql = "SELECT name, role, salary FROM employees";
- $sth = $dbh->query($sql);
- while ($row = $sth->fetchObject()) {
- $data[] = $row;
- }
- // close connection, clean up
- unset($dbh);
- // define template directory location
- $loader = new Twig_Loader_Filesystem('templates');
- // initialize Twig environment
- $twig = new Twig_Environment($loader);
- //extension added
- $twig->addExtension(new Twig_Extensions_Extension_Text());
- // load template
- $template = $twig->loadTemplate('employees.html');
- // set template variables
- // render template
- echo $template->render(array (
- 'data' => $data
- ));
- } catch (Exception $e) {
- die ('ERROR: ' . $e->getMessage());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement