jlittlejohn
5/31/2013 - 2:27 PM

PHP: PHP Data Object Class

PHP: PHP Data Object Class

<?php  
  try{
    /* Connect to Database */
    $dsn = 'mysql:dbname=test;host=localhost';
    $dbh = new PDO($dsn,'username','password');
 
    /* Create SQL String with parameters */
    $sql = 'SELECT name, colour, calories
   FROM fruit
   WHERE calories < :calories AND colour = :colour';
    $sth = $dbh->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    /* add values and execute request on database */
    $sth->execute(array(':calories' => 150, ':colour' => 'red'));
    /* Get the returned Data */
    $dataSet = $sth->fetchAll();
    /* Print Results */
    print_r($dataSet);
   
  }catch (PDOException $e){
    echo 'PDO Error: '.$e->getMessage();
  }catch(Exception $e){
    echo 'A Unknown Error Occurred: '.$e->getMessage();
  }
?>