International Developer Logo Last Updated 27.08.08 at 11.48
On Sale
This months front cover, click to see the table of contents.
Subscribe
 
TUTORIALS

Developing Three-Tier Web Applications with PHP


Daniel Winter   01.10.05

This class will be able to work directly with the objects that the PEAR DB class returns from its query method. In fact, so long as the variables are named identically, you will be able to assign each row object to an application object by reference. In other words, PEAR DB will return objects that can be manipulated directly by the application. Take for example the following method:

require_once 'DB.php';
//function to go in include file to create db connection
function getDbConn(){
      $user = 'trainingUser';
      $pass = 'qpwo1029';
      $host = 'localhost';
      $db_name = 'onlineTraining';
      $dsn = "mysql://$user:$pass@$host/$db_name";
      $db = DB::connect($dsn);
     
      if (DB::isError($db)) {
          die ($db->getMessage());
      }
      return $db;
 }   
// trainingsubject class 
class trainingsubject
{
   var $id;
   var $subject;
   var $teacherId;                          
   function select($theid){
      $db = getDbConn();
      $sql = "select id,subject,teacherid
from subjects where id = ?";
      $data = array($theid);
      $res =& $db->query($sql, $data);
      if (DB::isError($res)) {
          die ($res->getMessage());
      }
      if($res->fetchInto($row, DB_FETCHMODE_OBJECT)){
  //assign this object's properties to
//row object properties
       $this->id          =  $row->id;
       $this->subject     =  $row->subject;
       $this->teacherid   =  $row->teacherid;
      }      
   }
   function insert($newname, $newteacherid){
      $db = getDbConn();
      $sql = "insert into students set name = ?,
teacherId = ?";
      $data = array($newname, $newteacherid);
      $db->query($sql, $data);
      if (DB::isError($result)) {
          die ($result->getMessage());
      }
   }
   //other methods to go here
}




   Previous Page  1 2 3 4 5 6 7 8 9 10 ... Next Page   

HAVE YOUR SAY
This article is rated  Rate this article