Tuesday, October 31, 2017

PDO::FETCH_CLASS VS. PDO::FETCH_PROPS_LATE

PDO PHP Fetch Class

PDO::FETCH_CLASS

In our last post we have discussed about PDO::FETCH_CLASS, from which I think you are able to build complete understanding on PDO::FETCH_CLASS. But in this post we are going to discuss about few shortcomes about PDO::FETCH_CLASS.

Lets see the below example for PDO::FETCH_CLASS.

<?php
// < Class : user >
// class consists of userdefine functions and variables
class user{
 
 private $USERNAME ;
 private $EMAILID ;
 private $AGE ;
 private $COUNTRY ;
 //get the username
 public function getUSERNAME(){
  return $this->USERNAME ;
 }
 //get the email id
 public function getEMAILID(){
  return $this->EMAILID ;
 }
 //get the age
 public function getAGE(){
  return $this->AGE ;
 }
 //get the country
 public function getCOUNTRY(){
  return $this->COUNTRY ;
 }
 public function __construct(){
  $this->USERNAME = "sumit@gmail.com";
  
 }
 
}

// < Class : DAO >
// Get the data from database...
Class DAO{

//function return the all the data from database...
 public function getAllUserDetails(){

$dbhost ="localhost"; // set the hostname
$dbname ="skptricksdemo" ; // set the database name
$dbuser ="root" ; // set the mysql username
$dbpass ="";  // set the mysql password
 
try { 
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); 
$dbConnection->exec("set names utf8");
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully.<br>"; 

$stmt = $dbConnection->prepare('SELECT * FROM `userdetails` WHERE `AGE` > 10 ');
$stmt->execute();

$Count = $stmt->rowCount(); 
echo " Total Records Count : $Count .<br>" ;

if ($Count  > 0){
$stmt->setFetchMode(PDO::FETCH_CLASS, "user");

return $obj = $stmt->fetchAll(); 

}

}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
 
}
}


// calling the getAllUserDetails function and retrieve details 
$dao = new DAO();
$allUserDetails =$dao->getAllUserDetails();

//echo print_r($allUserDetails);
foreach($allUserDetails as $user)
{
 //echo print_r ($UneNews);
 echo "<pre>".$user->getUSERNAME()."</pre>" ;
 //echo "<pre>".$user->getEMAILID()."</pre>" ;
 
 
}

?>

OUTPUT:
-------------------------------
Connected successfully.
Total Records Count : 4 .
sumit@gmail.com
sumit@gmail.com
sumit@gmail.com
sumit@gmail.com

If you have clearly observe the code and their corresponding output, Then you can find the shortcomes. In this above example we are trying to retrieve username from database (e.g. $user->getUSERNAME() ) and our end result for username is same for all, but in this database values are different. So the reason is that  the data is populated into the object first, and then the constructor is called. Here constructor overwritten the same values every time.

In some situation we may require to call constructor first and then setting the values to object. So in that case we need to use PDO::FETCH_PROPS_LATE. Basically it overcomes the problem of  PDO::FETCH_CLASS.

PDO::FETCH_PROPS_LATE

Lets see the below examples, where we are retrieving values PDO::FETCH_PROPS_LATE  mode.
<?php
// < Class : user >
// class consists of userdefine functions and variables
class user{
 
 private $USERNAME ;
 private $EMAILID ;
 private $AGE ;
 private $COUNTRY ;
 //get the username
 public function getUSERNAME(){
  return $this->USERNAME ;
 }
 //get the email id
 public function getEMAILID(){
  return $this->EMAILID ;
 }
 //get the age
 public function getAGE(){
  return $this->AGE ;
 }
 //get the country
 public function getCOUNTRY(){
  return $this->COUNTRY ;
 }
 public function __construct(){
  $this->USERNAME = "sumit@gmail.com";
  
 }
 
}

// < Class : DAO >
// Get the data from database...
Class DAO{

//function return the all the data from database...
 public function getAllUserDetails(){

$dbhost ="localhost"; // set the hostname
$dbname ="skptricksdemo" ; // set the database name
$dbuser ="root" ; // set the mysql username
$dbpass ="";  // set the mysql password
 
try { 
$dbConnection = new PDO("mysql:host=$dbhost;dbname=$dbname", $dbuser, $dbpass); 
$dbConnection->exec("set names utf8");
$dbConnection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Connected successfully.<br>"; 

$stmt = $dbConnection->prepare('SELECT * FROM `userdetails` WHERE `AGE` > 10 ');
$stmt->execute();

$Count = $stmt->rowCount(); 
echo " Total Records Count : $Count .<br>" ;

if ($Count  > 0){

$stmt->setFetchMode(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE, "user");
return $obj = $stmt->fetchAll(); 

}

}
catch (PDOException $e) {
echo 'Connection failed: ' . $e->getMessage();
}
 
}
}


// calling the getAllUserDetails function and retrieve details 
$dao = new DAO();
$allUserDetails =$dao->getAllUserDetails();

//echo print_r($allUserDetails);
foreach($allUserDetails as $user)
{
 //echo print_r ($UneNews);
 echo "<pre>".$user->getUSERNAME()."</pre>" ;
 //echo "<pre>".$user->getEMAILID()."</pre>" ;
 
 
}

?>

OUTPUT:
-----------------------
Connected successfully.
Total Records Count : 4 .
Rahul
Mark
Mark
Wayplus



No comments:

Post a Comment