How to use PHP PDO Fetch Class

php pdo fetch class


In this tutorial, you will learn how to use and create the PHP PDO fetch class mode (PDO::FETCH_CLASS ) to fetch data from the database.

Before entering in this tutorial, you will need to learn and read the php fetch pdo and examples.

Here, I just created a database and named it a PHP tutorial and create a table, and named a student.




Create table student
(
id int not null AUTO_INCREMENT,
student_name varchar(50) not null,
dept_name varchar(50) not null
);
  


so, the student's class name is php. php fetch class returns a new instance of the requested class, each row in the student table mapping the columns of the result set to named properties in the student class.

 

 $ads={1}

 

Read also:

How to add html responsive table in blogger and wordpress

How to create a accordion ui design Examples in html

8 Best Free Keyword Research Tools for SEO

What is domain and web hosting

Now connect the php pdo connection database using the dbconfig.php script:



<?php
$db_host="localhost";
$db_user="root";	
$db_password="";	
$db_name="phptutorial";	

try
{
	$db=new PDO("mysql:host={$db_host};dbname={$db_name}",$db_user,$db_password);
	$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOEXCEPTION $e)
{
	$e->getMessage();
}

?>




<?php

class student
{
}

 require 'dbconfig.php';

$sql = 'SELECT student_name,dept_name
        FROM student'
 ;

$stmt = $db->prepare($sql);
$stmt->execute();
$stmt->setFetchMode(PDO::FETCH_CLASS, 'student');
$student = $stmt->fetch();

var_dump($student);

?>


Output:



object(student)#3 (2) 
{ 
["student_name"]=> string(12) "chandrakumar"
["dept_name"]=> string(3) "cse" 
}


In the above code, using fetch() and it's select a first row from the student table.


$stmt->setFetchMode(PDO::FETCH_CLASS, 'student');


Above the code format that is set to the fetch mode and passes the student class name to the second argument of the setFetchMode() method and follows the code and executes the statement to get the result set.

Assigning class properties:


if there are some rules to be followed, which method you will be assigning the columns values to the object will be returned by the query to the corresponding class properties.

it the student class has a property, which name is the same as a column name and it's assigned the column value to the property.

if the class property has no property the php will call the magic method to __set() the value to call and execute.

If there is no __set() method and the php will create a public property to call the value.



<?php
require 'dbconfig.php';
class studentclass
{
    private $student_name;
	private $dept_name;

   public function __set($name,$dept)
  {
    echo $this->student_name .'<br/>';
	echo $this->dept_name;
    }
}
$sql= "SELECT student_name,dept_name FROM student where id = :id";
$stmt = $db->prepare($sql);
$stmt->execute([':id' => 1001]);
$stmt->setFetchMode(PDO::FETCH_CLASS, 'studentclass');
$student = $stmt->fetch();
var_dump($student);
?>

Output:


object(studentclass)#3 (2) { 
["student_name":"studentclass":private]=> string(12) "chandrakumar" 
["dept_name":"studentclass":private]=> string(3) "cse" 
}


<?php
class student
{
    private $student_name;
	private $dept_name;

    public function getdetails()
    {
        echo $this->student_name .'<br/>';
		echo $this->dept_name;
    }
}

$stmt = $db->query("SELECT * FROM student");
$stmt->setFetchMode(PDO::FETCH_CLASS, 'student');
$student = $stmt->fetch();
$student->getdetails();
?>

Output:



chandrakumar
cse

Assigning Object:



<?php
require 'dbconfig.php';
class student
{
    private $student_name;
   private $dept_name;

   public function getdetails()
  {
    echo $this->student_name .'<br/>';
	echo $this->dept_name;
    }
}
$sql= "SELECT student_name,dept_name FROM student where id = :id";
$stmt = $db->prepare($sql);
$stmt->execute([':id' => 1001]);
$stmt->setFetchMode(PDO::FETCH_CLASS, 'student');
$student = $stmt->fetch();
$student->getdetails();
?>

Output:



chandrakumar
cse


In the above example code , student class has two private properties and an __set() method.

here, the private properties are student_name,dept_name with the columns name from the selected row,and it's php assings the columns values to these properties.

Assigning object properties and calling constructor:


In the below code, PDO assigns column values to the object properties before calling the constructor and get the result on getdetails() and fetch the data from the id and mention the id value as ':id' => 1001.



<?php
require 'dbconfig.php';

class student
{
    private $student_name;
	private $dept_name;

   public function __construct()
  {
    $this->getdetails();
    }
  public function getdetails()
  {
	echo $this->student_name .'<br/>';
	echo $this->dept_name;
  }
}
$sql= "SELECT student_name,dept_name FROM student where id = :id";
$stmt = $db->prepare($sql);
$stmt->execute([':id' => 1001]);
$stmt->setFetchMode(PDO::FETCH_CLASS, 'student');
$student = $stmt->fetch();
$student->getdetails();
?>

Output:


chandrakumar
cse chandrakumar
cse

Previous Post Next Post