Sunday, October 29, 2017

PDO::FETCH_CLASS Example

PDO PHP Fetch Class using PDO::FETCH_CLASS

PDO::FETCH_CLASS helps to maintain the direct relation with class attribute. Basically Returns a new instance of the requested class, mapping the columns of the result set to named properties in the class.
Another useful mode is PDO::FETCH_CLASS, which can create an object of particular class and no need of extra line of codes.

$stmt->setFetchMode(PDO::FETCH_CLASS"user");
return $obj $stmt->fetchAll(); 
$obj variable will produce an array filled with objects of News class, setting class properties from returned values.

"Userdetails" Table details :


Lets see the complete example where we are retrieving the complete users details.

<?php

//create the user class to retrieve value from database.
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 ;
 }
 
}

//function return the all the data from database...
 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 

$allUserDetails = 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
Mark@gmail.com
Mark@gmail.com
wayplus@gmail.com


PDO::FETCH_CLASS Example Using OOP Concept :


UserClass.php
Simple class with user defined variables and functions.
<?php

//create the user class to retrieve value from database.
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 ;
 }
 
}
?>

DAO.php
This class retrieve values from database using PDO::FETCH_CLASS mode.
<?php
//create the user class to retrieve value from database.
include("UserClass.php");

class DAO{
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();
}
}

}

?>

main.php
Here we are calling function to retrieve data and displaying the content in browser screen.
<?php
include("DAO.php");

$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
Mark@gmail.com
Mark@gmail.com
wayplus@gmail.com



No comments:

Post a Comment